# .isdigit() problem

Page 1 of 1

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

### #1 bess394

• New D.I.C Head

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 don't understand can someone please help?
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: 1456
• Posts: 4,716
• 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.
Was This Post Helpful? 1

### #3 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,804
• 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)
Was This Post Helpful? 0

### #4 bess394

• New D.I.C Head

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
Was This Post Helpful? 0

### #5 sepp2k

• D.I.C Lover

Reputation: 2579
• Posts: 4,114
• 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).
Was This Post Helpful? 0

### #6 Marbelous

• D.I.C Head

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!
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }