5 Replies - 318 Views - Last Post: 08 November 2017 - 07:23 AM Rate Topic: -----

#1 bess394  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

:code:
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  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,308
  • 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  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10681
  • View blog
  • Posts: 18,289
  • 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  Icon User is offline

  • New D.I.C Head

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

Re: .isdigit() problem

Posted 07 November 2017 - 02:49 PM

View Postsnoopy11, 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,982
  • Joined: 21-June 11

Re: .isdigit() problem

Posted 08 November 2017 - 03:19 AM

View Postjon.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  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 132
  • 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! :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1