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 ma
raw_input() returns a string:
>>> credits = raw_input("> ")
> 150
>>> type(credits)
<type 'str'>
You need to cast it to int
:
credits = int(raw_input("> "))
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
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"
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.