easy_c0mpany80
easy_c0mpany80

Reputation: 337

Python if statement not working correctly

Im a very new Python user (2.7) and have been working my way through the Learn Python The Hard Way course and up to chap 37 and decided to do read through some other learning materials and go over the basics again and do exercises there. I have been reading through this:

http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/ifstatements.html

and I just did this:

3.1.4.1. Graduate Exercise
Write a program, graduate.py, that prompts students for how many credits they have. Print whether of not they have enough credits for graduation. (At Loyola University Chicago 120 credits are needed for graduation.)

and this is my code for that:

print "How many credits do you currently have: "
credits = raw_input("> ")
if credits >= 120:
   print "You have graduated!"
else:
   print "Sorry not enough credits"

but no mater what number I input it just gives "Sorry not enough credits" as the answer every time, why? I have tried moving some things around and making it > instead of >= but nothing worked. Im sure this is something stupidly simple Im missing but I cant figure it out.

I've done several other if statement exercises in the LPTHW course similar to this and never had a problem.

Upvotes: 0

Views: 389

Answers (4)

Shakeel
Shakeel

Reputation: 2035

You need to accept integer input for that and also need to handle non integer inputs.

print "How many credits do you currently have: "
try:
    credits = int(raw_input("> "))
    if credits >= 120:
        print "You have graduated!"
    else:
        print "Sorry not enough credits"
except ValueError:
    print "Invalid input"

Output:

> 100
Sorry not enough credits

> 121
You have graduated!

> aaaa
Invalid input

Upvotes: 0

Aman
Aman

Reputation: 1

I am refering to the same material by Dr. Andrew Harrington and I am doing the same program, my program may look pretty much amatuerish so I would highly appreciate if someone can kindly refine it

def graduateEligibility(credits):
    if credits >= 120:
        print("Congratulations on successfully completing the course.....see you on graduation day!")
    else:
        print("Sorry! Your credits are below 120, please kindly retake the evaluaton tests")


def main():
    E = float(input("Enter your English marks:"))
    M = float(input("Enter your Mathematics marks:"))
    P = float(input("Enter your Physics marks:"))
    C = float(input("Enter your Chem marks:"))
    Cf = float(input("Enter your Comp.Fundamentals marks:"))
    Fin_Edu = float(input("Enter your finance marks:"))
    Const = float(input("Enter your Constitutional.Stds marks:"))
    R = float(input("Enter your Reasoning marks:"))
    TotalCredits = (E+M+P+C+Cf+Fin_Edu+Const+R)
    YourCredits = graduateEligibility(TotalCredits)
main()

For simplicity sakes i have taken 8 subjects each have 20 credits.

Upvotes: 0

alecxe
alecxe

Reputation: 474161

raw_input() returns a string:

>>> credits = raw_input("> ")
> 150
>>> type(credits)
<type 'str'>

You need to cast it to int:

credits = int(raw_input("> "))

Upvotes: 4

salmanwahed
salmanwahed

Reputation: 9657

In your code, at the if statement you are comparing a str type with a int type. so it is not working as you axpected. Cast the credit as int

print "How many credits do you currently have: "
credits = raw_input("> ")
credits = int(credits)
if credits >= 120:
   print "You have graduated!"
else:
   print "Sorry not enough credits"

Upvotes: 0

Related Questions