[Tutor] Assistance

Alan Gauld alan.gauld at btinternet.com
Sun Apr 24 17:38:41 CEST 2011


"Krystal Brosz" <daisyk82 at gmail.com> wrote

> i'm struggling with a program, i feel like i am really close to 
> getting it

You are not far away but you have a few little
problems to fix.

> but i cannot find a way to use the target variables inside of a 
> loop:

I have no idea what you mean by "target variables".
There are no variables in the code called 'target' and
I don't understand what target you mean?

> def main():
>    gradesEntered = 0
>    score = 0
>    numberOfGrades = 0
>    numberOfGrades = int(raw_input("Please enter the number of 
> grades:" ))
>
> #Begin a 'for' loop to enter each score

The comment is confusing because you actually enter a while loop.
Inaccurate comments are worse than no comments at all!

>    while numberOfGrades != gradesEntered:
>        grade = int(raw_input("Please enter the grade:" ))
>        gradesEntered += 1
>        score =+ grade

Note that += and =+ do different things. I suspect this last line is
not doing what you think. Details like this are very important in
programming, especially since both forms are valid code, they
just do different things!

>        grade = [numberOfGrades]

And I don't know what you think this is doing but it is in fact
overwriting the value of grade that the user entered with a list
of one element, numberOfGrades. You then throw this away
the next time round the loop.

>    for i in range (numberOfGrades):
>        grade.append(i)
>        print determine_grade(grade,i)

Now you append i to the list containing numberOfGrades....

So if the user said they would have 3 grades you wind up
with grade being equal to

[3,0,1,2]

Which is almost certainly not what you want?
You also pass this list to determine_grade which
expects a number rather than a list so will always
print an error I suspect. Although I'd also expect it to
throw an NameError exception since result will not be defined...
Are you sure this is the actual code you are running?

> #get the grade letter for scores
>    print "the total number of grades entered are:", score
>    print "The average of the scores is:", calc_average
>    print "The number of grades entered is:", gradesEntered

So ths prints the value of yourt last grade multiplied by  +1
followed by a calc_average which is not actually set
anwthere in your code. It then prints gradesEntered
which should be the same as numberOfGrades.

> def determine_grade(grade,i):

I'm not sure why you have i in the parameter list?
You don't do anything with it.

>        if grade >= 90 and grade <= 100:
>              result = 'Your grade is A'
>        elif grade >=80 and grade <= 89:
>              result = 'Your grade is B'
>        elif grade >= 70 and grade <= 79:
>              result = 'Your grade is C'
>        elif grade >=60 and grade <= 69:
>              result = 'Your grade is D'
>        elif grade < 60:
>              result = 'Your grade is F'
>        else:
>            print 'Error: Invalid grade.'
>        return result

If you reach the last 'else', result will not have been
assigned and yet, you try to return it. I'd expect
that to throw an error.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list