Python Problems

Started by
7 comments, last by Zahlman 18 years, 7 months ago
a = input("Give me a number... ")
b = input("And one more number... ")
input("I am now adding those two numbers together...")
If a + b > 100:
   print "Wow! Thats above 100!!"
elif a + b < 100:
   print "Uh oh! Thats below 100... although it makes no difference!"
elif a + b = 100:
   print "Hey! Thats EXACTLY 100!"
else:
   print "Hrmmmm... you may want to try that again, it seems we've got a problem!"

I'm learning Python right now, and have reached the if statements, however, when I try to run it, I get a syntax error. When I run it through the given Python Package, it highlights the a in, If a + b > 100: So my guess, is that all the other variables will have problems too, as the given package only highlights one problem at a time. BTW: Im programming this in a text editor, that supports indents and color coding called ConTEXT, but it doesn't have a debugger, are there any good python debuggers out there, so I dont have to port it to the python package thing if I get an error?
Advertisement
That's because Python is case sensitive, and you misspelled if as If.
a = input("Give me a number... ")b = input("And one more number... ")input("I am now adding those two numbers together...")if a + b > 100:   print "Wow! Thats above 100!!"elif a + b < 100:   print "Uh oh! Thats below 100... although it makes no difference!"elif a + b = 100:   print "Hey! Thats EXACTLY 100!"else:   print "Hrmmmm... you may want to try that again, it seems we've got a problem!"


Ok, changed If to if, but it still doesn't work.
elif a + b == 100:
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Hey, Thanks!

Also to note, that it wasn't working after the =. == fixed, and so I found out, that I had to do raw_input("........"), if I just wanted a pause, because input gave me a problem when I just hit enter, it wanted a number I guess.

BTW: What exactly, is ==? Whats the difference between using that and just simple =?
The '=' is the assignment operator -- notice that you use it to assign a to the returned value of input(). '==' is the operator to check equivalence, "is equal to".
Things change.
Also, raw_input gives you a string and you want a number. For inputing numbers you should use a = int(raw_input("Gimme a number: "))

Oxyd
input() give you an int, so it's fine to use that.
Not quite. input() gives you the result of interpreting the input line as a Python expression. As such, you might not want to use it in production code.

Converting the raw_input result to an int will work fine as long as that's what the user put in. Otherwise it will raise ValueError; you may want to trap that.

print "Here is a menu"print "1 - do something!"print "2 - do something else!"while True:  try:    selection = int(raw_input("give me a blasted number!"))    if selection in (1, 2): break # we got what we need  except ValueError: # something that wasn't a number got input    continue # try againprint "You now have %d widgets." % (selection * 42)

This topic is closed to new replies.

Advertisement