help with a if statement

This is the place for queries that don't fit in any of the other categories.

help with a if statement

Postby amber555 » Tue Dec 17, 2013 3:53 am

Okay, I am relatively new to Python and I can't get something too easy to work. Any help would be appreciated. The else statement doesn't work. Here is my code:

Code: Select all
def student_grade():
    grade=raw_input('please enter your grade')
    first_name=raw_input('please enter your name')
    if grade >=70:
        print(first_name + ' Congratulations!  You passed the class!')
        print(first_name + 'unfortunately you did not pass the class - try again')
Last edited by micseydel on Tue Dec 17, 2013 4:37 am, edited 1 time in total.
Reason: Code tags, lock.
Posts: 1
Joined: Tue Dec 17, 2013 3:50 am

Re: help with a if statement

Postby micseydel » Tue Dec 17, 2013 4:41 am

When you have
Code: Select all
if grade >=70:

your variable grade is a string. Python 2 unfortunately does something confusing here. (Python 3 thought better and give you a helpful error when you do something like this.) The fix for this is to change grade to a number. You can do so by changing
Code: Select all
grade=raw_input('please enter your grade')

Code: Select all
grade = int(raw_input('please enter your grade'))

or you can use float() in place of int() if you want to accept floating point numbers.

Also, you should read this before posting again. (Note I added code tags this time, I'd prefer not to a second time.)
Join the #python-forum IRC channel on for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from :)
User avatar
Posts: 2904
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: Ashokkumar_newbie, snippsat and 3 guests