[Tutor] Fwd: Assistance

Steven D'Aprano steve at pearwood.info
Sun Apr 24 14:20:40 CEST 2011


Krystal Brosz wrote:
> Hi there,
> 
> i'm struggling with a program, i feel like i am really close to getting it
> but i cannot find a way to use the target variables inside of a loop:
> I'm trying to get the program to ask the user, how many grades are you going
> to enter.  Then i want to work out the average which is fine.  But then i
> want the loop to print out from each grade entered the letter grade as per
> my if statement.  All it does is print the last grade out a certain number
> of times. Is this even possible or am i overthinking it?

No, you're not over-thinking it, but you do have a few small errors in 
your code:


> Some code is:
> def main():
> 
>     gradesEntered = 0
>     score = 0
>     numberOfGrades = 0
> #get the number of grades being checked
>     numberOfGrades = int(raw_input("Please enter the number of grades:" ))
> 
> #Begin a 'for' loop to enter each score
>     while numberOfGrades != gradesEntered:
>         grade = int(raw_input("Please enter the grade:" ))
>         gradesEntered += 1
>         score =+ grade
>         grade = [numberOfGrades]

Here you ask the user to enter a grade. Suppose they enter (say) 75. 
Your program will store 75 in grade, and then a moment later over-write 
that by storing [numberOfGrades] in grade.

I believe that what you need is to have a variable "grades" (note 
plural), and each time around the while loop, you need to append the 
current grade to the grades list.

Then, after you have collected all the grades, you can iterate over the 
list to get each grade one at a time:

for grade in grades:
     print grade


See how you go with that, and don't hesitate to ask if anything is unclear!





-- 
Steven


More information about the Tutor mailing list