[Solved] if value < var < value:

This is the place for queries that don't fit in any of the other categories.

[Solved] if value < var < value:

Postby mckryall » Sat Nov 23, 2013 7:08 pm

I am trying to make a simple if statement that returns if a value is too high, too low, or neither. I have tried three times to make it work and it still returns oddly.


Code: Select all
   name = raw_input("> ")
   if name < 1000:
      print "Too low. Try again."
   elif name > 9999:
      print "Too high. Try again."
   else:
      print "Good."

Returns: Too high. every time. Is it taking 1000 as a variable or something? I'm 99% sure that variables have to start with a letter.

Code: Select all
nope = raw_input("num: ")
if nope < 9999:
   if nope > 999:
      print "hi"
else:
   print "bye"

Returns: Bye. Every value I tried from 1-10000 returned that.


Conclusion:
I was using raw_input without changing it into and integer first. I should've done this:
Code: Select all
name = int(raw_input("> "))
if name < 1000:
   print "Too low. Try again."
elif name > 9999:
   print "Too high. Try again."
else:
   print "Good."
Last edited by micseydel on Sat Nov 23, 2013 8:26 pm, edited 2 times in total.
Reason: Made it more clear that the problem is solved.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 3 guests

cron