1

In the mentioned exercise there's this code:

from sys import exit

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

    next = raw_input("> ")
    if "0" in next or "1" in next:
        how_much=int(next)
    else:
        dead("Man, learn to type a number.")

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

In the 7th line, I want to put the regular expression [0-9] instead of 0 or 1, so that any inserted number would be passed to the next if statement, so I replaced it with:

    if next == [0-9]:

but after inserting any number I get the error message:

Man, learn to type a number.

I don't understand what's wrong.

Thank you for help

Amateur
  • 139
  • 1
  • 1
  • 6

2 Answers2

1

Try this:

try:
    how_much = int(next)
except ValueError:
    dead("Man, learn to type a number.")

Which will convert your input to an integer unless next is something that cannot be converted to an integer. Read this to learn more about errors and exceptions.

If you insist on using a regular expression, which you absolutely shouldn't:

if re.match("\d+", next):
    how_much = int(next)

Please don't use the regex.

msvalkon
  • 11,887
  • 2
  • 42
  • 38
  • `0 <= int(next) <= 9` – Burhan Khalid Feb 11 '14 at 09:52
  • Note that `range(0,10)` will construct a new list for every comparison, and using the `in` operator on that, has an average time complexity of `O(n)`. – moooeeeep Feb 11 '14 at 10:11
  • @moooeeeep yes, however for such a simple program it's not very relevant. Anyway I feel it's more pythonic to follow the EAFP here. – msvalkon Feb 11 '14 at 10:14
  • I mean you shouldn't teach novices bad idioms, even though it's not relevant in a toy example. – moooeeeep Feb 11 '14 at 10:19
  • @msvalkon pretty helpful, thanks. Couldn't I just go on with if instatement instead of try? – Amateur Feb 11 '14 at 10:35
  • @Amateur it's not a very good idea. The `try-except` is a common idiom in python and it's considered pythonic. It will also make sure that you can enter numbers like `500`. It does not limit you either, because you can still have the `if`'s afterwards. @moooeeeep has a very good point about the time complexity of the `in` operator (goes through all items in a sequence) and that the `range` function would generate a new list for each function call. – msvalkon Feb 11 '14 at 10:40
  • @msvalkon message received. Thanks. – Amateur Feb 11 '14 at 11:07
0

if you are particular about using regular expressions use this

import re
try:
  re.match(r"[0-9]", next).group()
  how_much = (int)next
except:
  dead("Man, learn to type a number.")
scarecrow
  • 6,624
  • 5
  • 20
  • 39
  • 1
    There are several problems with your code. One is that if you're going to handle the exception you might as well just do the conversion `how_much = (int)next` and catch any exception that throws. Another problem is that you should never, ever have a bare `except:`, you should only catch the expected exceptions that you know you can handle (sometimes that might be to catch `Exception` but usually only once per program). Finally, if you're going to use a regular expression then it would be clearer just to test the result rather than trying to force an `AttributeError`. – Duncan Feb 11 '14 at 10:32
  • @scarecrow It's a little difficult for me to understand it right now, but I think will be helpful in a not far future. Thanks – Amateur Feb 11 '14 at 10:37