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.)
Due to the reasons discussed here we will be moving to on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 11 guests