0

I am currently trying to learn Python 2.7 via Learn Python The Hard Way, but have a question about Study Drill 5 of Exercise 35.

The code I'm looking at is:

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

The study drill asks if there is a better way than checking if the number contains zero or one, and to look at how int() works for clues.

I obviously want to be able to accept any number given by the user while dismissing strings, and I understand that int() converts what was given in raw_input() into a number.

So, I tried a couple of ways of amending the if statement which threw up fatal errors when typing a string, but struggled to find anything suitable. I tried variations of the following code, and now understand why they don't work:

choice = int(raw_input("> "))

if choice > 0:

After searching SO I discovered this answer which gives two ways to solve the problem, however try...except and .isdigit() aren't something mentioned in the book at this point.

Is there any other ways to achieve taking user input, converting it to an integer if necessary, and returning an error if not that is appropriate for this stage of the book?

Community
  • 1
  • 1
tomdot
  • 551
  • 6
  • 20
  • 1
    I suppose you could look up the ascii codes with `ord(...)` – inspectorG4dget Aug 02 '14 at 13:26
  • are you checking if the input is 0 or 1 or if all input contains digits? – Padraic Cunningham Aug 02 '14 at 13:33
  • I don't think there's a better way than checking the input with `.isdigit()`. – miindlek Aug 02 '14 at 13:38
  • 2
    Related: http://stackoverflow.com/a/23294659/3001761. Also, note that `0 in 'foo0'`! – jonrsharpe Aug 02 '14 at 13:39
  • @PadraicCunningham: The check is an attempt to see if the input looks something like an integer prior to applying `int()` to it. Obviously it will accept the invalid input '10XRw' and reject the valid input '345'. So the question asks the user to come up with something better. – President James K. Polk Aug 02 '14 at 13:45
  • @inspectorG4dget ord(...) isn't something mentioned in the book at this point. – tomdot Aug 02 '14 at 13:46
  • @PadraicCunningham the code initially only checks whether the input contains 0 or 1, but then the study drill asks if there is a better way. I'm obviously looking for a way to accept any number, but reject any other string. – tomdot Aug 02 '14 at 13:48

3 Answers3

3

You can write your own is_digit function

def my_digit(input):
  digits = ['0','1','2','3','4','5','6','7','8','9']
  for i in list(input):
    if not i in digits:
       return False
  return True
Serjik
  • 10,543
  • 8
  • 61
  • 70
1

So, firstly read what jonrsharpe linked to in the comments and accept that try-except is the best way of doing this.

Then consider what it means to be an integer:

  • Everything must be a digit (no decimal point, too)

So that's what you check for. You want everything to be a digit.

Thus, for a represents_integer(string) function:

for every letter in the string:
    check that it is one of "0", "1", "2", ..., "9"
    if it is not, this is not a number, so return false

if we are here, everything was satisfied, so return true

Note that the check that is is one of might require another loop, although there are faster ways.


Finally, consider "", which won't work in this method (or GregS').

Community
  • 1
  • 1
Veedrac
  • 58,273
  • 15
  • 112
  • 169
  • Since you're explicitly doing this to learn Python, I tried to keep this pseudocode. Let me know if it doesn't work out. // EDIT: GregS basically has the code. ;) – Veedrac Aug 02 '14 at 13:47
  • My question isn't based on whether 'try-except' is the best way. I understand it is the best way, but try statements have not been introduced in the text at this point, so to me as a learner 'try-except' does not exist. I like this answer regardless, thanks. – tomdot Aug 02 '14 at 13:52
  • @tomdot Yup, I was just making sure :). – Veedrac Aug 02 '14 at 17:23
1

Since you already learned sets, you can test that every character is a digit by something like

choice = choice.strip()
for d in choice:
    if d not in "0123456789":
        # harass user for their idiocy

how_much = int (choice)
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125