Note that there are some explanatory texts on larger screens.

plurals
  1. POitems not appending to list, or list returning as empty in python?
    primarykey
    data
    text
    <p>In my code, the item percentgrade is not appending to the list lnpercentgrade in each iteration of a for loop, but I'm not sure why. When I try to sum the items in lnpercentgrade (at the end of the code), I am returned 0 because lnpercentgrade is empy. I have lnpercentgrades declared as a global variable outside of the maininput() function, because otherwise at the bottom of the code when I try to sum(lnpercentgrades), I am returned "lnpercentgrades not defined".</p> <p>I'm guessing lnpercentgrades is not being modified somehow inside the maininput() function, even though I am sure I typed it correctly before the .append function.</p> <p>What I am really trying to do is match the sum(lnpercentgrades) with 100, but when I enter 5 items all with value of 20, the sum is 0 instead of 100.</p> <p>The important lines are marked with arrows &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</p> <pre><code>lnpercentgrades = [] &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; def maininput(): numtests = int(input("Enter # of tests:")) numassign = int(input("Enter # of assignments:")) numquizzes = int(input("Enter # of quizzes:")) numlabs = int(input("Enter # of labs:")) l = [numtests, numassign, numquizzes, numlabs] ln = ["test", "assignments", "quizzes", "labs"] lnpercentgrades = [] &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; testwavg = None numassignwavg = None numquizzeswavg = None numlabswavg = None numfinalwavg = None lnwavg = [testwavg, numassignwavg, numquizzeswavg, numlabswavg] print(l[0]) print("Is there a separately weighted final?") yn = int(input("Enter 1 for yes. 2 for no:")) while (yn &gt; 2 or yn &lt; 1): yn = int(input("Enter 1 for yes. 2 for no, please:")) n = 4 if yn == 1: final = 1 l.append(final) ln.append("final") lnwavg.append(numfinalwavg) n = 5 for i in range (n): if l[i] &gt; 0: print("Enter % of total grade of",ln[i],":") percentgrade = float(input("")) print("you inputed:", percentgrade) lnpercentgrades.append(percentgrade) &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; percentgrade = percentgrade*.01 varscoreslist = [] for x in range(l[i]): print("Enter score of", ln[i], x+1,"as a fraction:") from fractions import Fraction inputfrac = input("") varscore = float(sum(Fraction(s) for s in inputfrac.split())) #the above converts fractional inputs to decimals varscoreslist.append(varscore) if ln[i] == "labs": score = sum(varscoreslist) else: sumscores = sum(varscoreslist) score = sumscores/l[i] lnwavg[i] = score*percentgrade maininput() print(lnpercentgrades) print("sum of percent of grades", sum(lnpercentgrades)) &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; if float(sum(lnpercentgrades)) != 100: &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; print("Sorry, the grade percent of each item enter did not sum to 100:") print("Program will rerun.") input("") maininput() coursegrade = sum(lnwavg) print("Your course grade is:", coursegrade) input("") </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload