-1

In LPTHW, Study Drill 5 for exercise 35 asks:

The gold_room has a weird way of getting you to type a number...Can you make it better than just checking if "1" or "0" are in the number? Look at how int() works for clues.

Here's the relevant gold_room code:

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!")

I tried using a list of numbers 0 through 9. Not exactly a "better" way, but I couldn't think of much else:

next = raw_input("> ")
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
if next in numbers:
    how_much = int(next)

As with "0" and "1" in the original code, I'd hoped each digit would function as a keyword. Unfortunately it didn't work for any number above 9.

I've searched for other solutions and found people using .isdigit(), try: and except ValueError: to solve the problem, but the book hasn't covered those yet, so I'd like to avoid using them. I'm looking for any other suggestions, especially something dealing with int() as the author mentioned. Thanks.

[edit]: This has been marked as a duplicate. It's not a duplicate. Read my responses and the question carefully; there are caveats. The answer that was linked uses try and other things I'm trying to avoid because they haven't been covered in the book yet.

TetraG
  • 11
  • 4
  • Have you learned for loops yet? – Alyssa Haroldsen Jul 24 '15 at 17:30
  • Yeah, for- and while-loops have been covered. – TetraG Jul 24 '15 at 17:32
  • When you do something like `next in numbers` it checks if `next` can be found in `numbers`. `raw_input` returns a *string* as the result. `numbers` currently contains only the single digits `'0'` to `'9'` *represented as strings*. Note that a *string* that represents a number with more than one digit, such as `'10'` won't be found. Hint: you probably want to be able to deal with numbers as actual number types, like *int* (instead of strings containing numbers) – shuttle87 Jul 24 '15 at 17:32
  • Or alternatively, check each consecutive character of the input string for being in the list of '0'-'9'. – mdurant Jul 24 '15 at 17:35
  • Removing the ' around the numbers, they become integers in the list, right? But even then, numbers above 9 won't work. I feel like I'm missing something really obvious, though... – TetraG Jul 24 '15 at 17:40
  • `['0','1','2']` is a list containing *strings*. `[0, 1, 2]` is a list containing *integers*. Do you understand the difference between the two? – shuttle87 Jul 24 '15 at 17:44
  • Yes. I know I have them as strings in the original question, and I said in my previous comment that removing the ' would turn them into integers. I get that, but it makes no difference in the output - I still get "Man, learn how to type a number!" when entering a number greater than 9. – TetraG Jul 24 '15 at 17:55

2 Answers2

0

This might help you along a bit:

numbers = '0123456789'
next = raw_input('> ')
isanumber = True
for character in next:
    if character not in numbers:
        isanumber = False
        break

This basically goes through each item in next (as a character) and will halt the for loop if it finds something that's not a number. If it was a number, the for loop will go all the way through and isanumber will still be True. If there was a non-number character in it (by checking if that character was in numbers), it will set isanumber to False and not bother to do any more checking.

A note though: next is actually a built-in function. It's generally a bad idea to have variables with the same name as built-in functions.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
  • Haven't come across `break` yet; any alternative? – TetraG Jul 24 '15 at 17:42
  • You can simply remove the `break` and it will be even less efficient, but it will still work. – Alyssa Haroldsen Jul 24 '15 at 17:43
  • Thanks. I'm still getting the wrong output when going above 9, though. Let me play with it some more... – TetraG Jul 24 '15 at 17:48
  • Yep, still no luck with this one. Thanks anyway. – TetraG Jul 24 '15 at 18:06
  • I asked for help on another forum and it looks like your code is good, but was missing something. The other person did pretty much the same thing, but added the two lines at the bottom: `if isanumber:` and `how_much = int(next)` I'd +1 your answer, but I don't have enough rep yet. Thanks for your help. – TetraG Jul 24 '15 at 19:24
  • I never said it was a complete answer; I was just trying to give a good hint. I believe you're still able to mark answers as completed? – Alyssa Haroldsen Jul 24 '15 at 19:30
  • Yup, done. Thanks again. – TetraG Jul 24 '15 at 19:38
0

int() would throw an exception when casting fails, so you could:

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

No need to have a list of numbers to iterate through, etc.

EDIT: To be clear in the function this would be:

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

    next = raw_input("> ")
    try:
        how_much = int(next)
    except:
        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!")
Maelstrom
  • 443
  • 2
  • 9
  • Thanks, but `try` hasn't been covered yet in the book, so I'd prefer not to jump ahead. – TetraG Jul 24 '15 at 17:41
  • The instruction specifically asked if you can detect numbers, not just 0-9 or whatever, and it specifically said to look at how int() works. Well, that's how int() works, and all the clues (in the code, ie. not a number) point to exception. Clearly it wants you to learn the hard way about exception. – Maelstrom Jul 24 '15 at 21:34