2

I'm trying to take a raw input and detect whether it is in a range.
Here's my code.

def gold_room():
    print "This room is full of gold. How much do you take?"

    next = raw_input("> ")
    if next == int in range(50):
        how_much = int(next)
    else:
        dead("Man, learn how to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")

When I enter a number it gives me the else: "Man, learn how to type a number."
I guess the line that isn't working is "if next == int in range(50):
Could anyone help me out?
Thanks in advance!
Edit:
I'm a noob so that line was just me ballparking.
I thought it would check next to see if it was an integer in the range of numbers 0-50.

3
  • 3
    Please explain what you think next == int in range(50) will do. Please update the question with a step-by-step explanation for this expression. Perhaps you should try it at the >>> prompt to be sure of what it does.
    – S.Lott
    Feb 24, 2012 at 20:49
  • 1
    Don't shadow the built-in function next.
    – Katriel
    Feb 24, 2012 at 21:04
  • "that line was just me ball parking". Bad habit. use the >>> prompt, please to confirm what the code does (or does not) do.
    – S.Lott
    Feb 24, 2012 at 22:13

5 Answers 5

3

If you want to "get an integer input in a range", you'll need two things:

  1. Check if the input is an int
  2. Check if it's in your range

Check if the input is an int

try/except will be perfect here:

n = raw_input('> ')
try:
    n = int(n)
except ValueError:
    dead() # or what you want

Why this is good?
Because if n is an int you'll have it convert it to an integer and if it's not an exception get raised and you can call your dead() funcion.

Check if it's in your range

If you get to this point it means that the exception before it was not raised and n was converted to an integer.

So you just need to do:

if 0 <= n <= 50:
    print 'You win'
else:
    print 'You lose'

Don't do:

if n in range(50):
    # ...

Beacuse it will build a list of 50 numbers for nothing.

Note: don't use next as a variable, beacuse it'll shadow the built-in next()

1

Since raw_input returns a string, you need to convert to an int first. Try replacing the line with this:

if int(next) in range(50):
2
  • 3
    This is not a good way to check if a number is between 0 and 50.
    – Rik Poggi
    Feb 24, 2012 at 21:53
  • Ditto. There are much better ways to check this. Feb 24, 2012 at 22:32
0

The result of raw_input() will be a string, so first you need to check to see if next is all digits. There are a few ways to do this, the easiest is to use the str.isdigit() function:

next = raw_input("> ")
if next.isdigit():
    how_much = int(next)
else:
    dead("Man, learn how to type a number.")

Note that you do not need to check to see if the value for next is in the range from 0 to 50, since your next if statement already checks to see if the value is less than 50 and negative numbers will be excluded by next.isdigit().

1
  • EAFP: try: int(next) except ValueError: dead(...)
    – Katriel
    Feb 24, 2012 at 21:05
0

The test "next == int in range(50)" evaluates to "(next == int) and (int in range(50))" which is fairly meaningless and always equates to false.

Instead you could try

try:
    how_much = int(next)
    if not (0<=how_much<50):
       print 'Too greedy'
except ValueError:
    dead("Man, learn how to type a number.")
0

Using try .. except will allow you to make sure entered value is an int. # raise is a place holder for your handling a non-int contition:

try:
    next = int(raw_input("> "))
except ValueError:
    # raise

if not 0 <= next <= 50:
    print 'Too greedy'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.