[Tutor] Python help

Alan Gauld alan.gauld at btinternet.com
Mon Apr 2 20:00:59 EDT 2018


On 02/04/18 23:44, Shannon Evans via Tutor wrote:
> Hi, I am trying to write a code with if statements but the code keeps just
> repeating and not carrying on.

There are quite a few problems here, see comments below.

> while True:
>     try:
>         Grade = int(raw_input("Please enter your Grade: "))

You are trying to convert the input to an integer.
But A-F will not convert so thats the first problem
right there.

>     except ValueError:
>         print("Error, Please enter A, B, C, D, E or F")
>         continue

And here you prompt for an A-F input not an integer.


>     if Grade <> 'A','B','C','D','E','F':
This doesn't do what you think. It tests to see if Grade
is not equal to a tuple of values ('A','B','C','D','E','F')
You need to use 'in' instead. That will check whether
or not Grade is *one* of the values in the tuple.

    if Grade not in 'A','B','C','D','E','F':

>         print ("Error, Please enter A, B, C, D, E or F")
>         continue
>     else:#Grade succesfully completed, and we're happy with its value.
>         #ready to exit the loop.
>         break

Taking out the conversion to int() would be a good start.
Then change the if test to use 'in'.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list