Sign in to follow this  

Python Problems

This topic is 4517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 =?

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
input() give you an int, so it's fine to use that.

Share this post


Link to post
Share on other sites
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 again
print "You now have %d widgets." % (selection * 42)

Share this post


Link to post
Share on other sites
Sign in to follow this