# .isdigit() problem

Page 1 of 1

## 5 Replies - 399 Views - Last Post: 08 November 2017 - 07:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407429&amp;s=4bbc02c449e45f5538e99ab2da64817e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bess394

Reputation: 0
• Posts: 2
• Joined: 06-November 17

# .isdigit() problem

Posted 06 November 2017 - 02:03 PM

Hi I am just starting to learn python and I picked up a guide book to help walk me through the basics. I'm stuck as I have been copying some code as the book says to create a number guessing game but I am having trouble with .isdigit()
here is the code exactly as it shows in the book
```import random
num = random.randint(1,20)
flag = True
guess = 0
print('Guess a number from 1-20:' , end = '')
while True:

quess = input()
if not guess.isdigit():
print('Invalid! Enter only digits 1-20')
break
elif int( guess) < num:
print('Too low, try again:' , end ='')
elif int( guess) > num:
print('Too high, try again:' , end ='')
else:
print('correct... My number is ' + guess)
flag = False

```

when running this code I get this

= RESTART: C:/Users/JOSH/AppData/Local/Programs/Python/Python36-32/guess.py =
Guess a number from 1-20:ten
Traceback (most recent call last):
File "C:/Users/JOSH/AppData/Local/Programs/Python/Python36-32/guess.py", line 9, in <module>
if not guess.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'
>>>

I am using python 3.6 on windows 10

This post has been edited by modi123_1: 06 November 2017 - 02:13 PM
Reason for edit:: In the future, please use the [code] tag button in the editor.

Is This A Good Question/Topic? 0

## Replies To: .isdigit() problem

### #2 snoopy11

• Engineering ● Software

Reputation: 1467
• Posts: 4,726
• Joined: 20-March 10

## Re: .isdigit() problem

Posted 06 November 2017 - 03:52 PM

Look at line 8 and see if you're spelling is ok ?

And .isdigit() is for strings ie guess ="" not int's which are bound to be digits by default.

### #3 jon.kiparsky

• Beginner

Reputation: 11095
• Posts: 18,982
• Joined: 19-March 11

## Re: .isdigit() problem

Posted 06 November 2017 - 04:44 PM

I don't see any way for the code that you're showing to produce those results, since input will always produce a string.
Are you sure the code you've shown is the code you're running in your interpreter?
(it's possible that you made changes but didn't refresh them in your interpreter)

### #4 bess394

Reputation: 0
• Posts: 2
• Joined: 06-November 17

## Re: .isdigit() problem

Posted 07 November 2017 - 02:49 PM

snoopy11, on 06 November 2017 - 03:52 PM, said:

Look at line 8 and see if you're spelling is ok ?

And .isdigit() is for strings ie guess ="" not int's which are bound to be digits by default.

Thanks snoopy11 feel like a right plonker cant believe i didnt spot that. Thanks

### #5 sepp2k

• D.I.C Lover

Reputation: 2627
• Posts: 4,182
• Joined: 21-June 11

## Re: .isdigit() problem

Posted 08 November 2017 - 03:19 AM

jon.kiparsky, on 07 November 2017 - 12:44 AM, said:

input will always produce a string.

This is true in Python 3, but in Python 2 input reads a Python expression, evaluates it and returns the result. So if you enter an integer, you'll get back an integer (and if you enter a string without quotes, you'll get back the content of some variable, an undefined variable error or a syntax error depending on the string).

### #6 Marbelous

Reputation: 43
• Posts: 136
• Joined: 12-November 12

## Re: .isdigit() problem

Posted 08 November 2017 - 07:23 AM

It looks like he is using Python3. The reason the error indicates "guess" is an int is because he initialed it to a zero in line 4. The code should work as is but...

1) It doesn't end when you win. Break out of the loop after a correct answer.

2) It should really check the input with isdigit() AND confirm the digit is between 1 - 20.

Good job though bess394!