简体   繁体   English

为什么我的代码不起作用 AttributeError: 'int' object has no attribute 'isdigit'

[英]why my code not working AttributeError: 'int' object has no attribute 'isdigit'

#Guess the num
import random
def is_valid_num(num):
    if num.isdigit() and 1 <= int(num) <= 100:
        return True
    else:
        return False

def main():
    number = random.randint(1,100)
    guessed_number = False
    guess = input('enter a num')
    #guess = (input('enter a num'))
    num_of_guesses = 0
    while not guessed_number:
        if not is_valid_num(guess):
            #return False
            guess = input('i count only digits enter 1<num<100')
            continue
        else:
            num_of_guesses += 1
            #break
            guess = int(guess)
        if guess < number:
            print ('entered number is low')

        elif guess > number:
            print ('entered number is high')

        else:
            print ('you got in',num_of_guesses, 'guesses')
            guessed_number = True
main()

Expected Out if random number is system is 51 and we pressed 50 it will print too low, then continue this process lets say we gave input 51 output will you got in 2 guesses如果随机数是系统 51 并且我们按下 50 它将打印得太低,然后继续这个过程假设我们输入 51 output you got in 2 guesses

isdigit() is a string method, it doesn't work on int inputs. isdigit()是一个字符串方法,它不适用于int输入。

change this:改变这个:

guess = int(input('enter a num'))

to this:对此:

guess = input('enter a num')

your code after editing:编辑后的代码:

#Guess the num
import random
def is_valid_num(num):
    if num.isdigit() and 1 <= int(num) <= 100:
        return True
    else:
        return False

def main():
    number = random.randint(1,100)
    guessed_number = False
    guess = input('enter a num')
    #guess = (input('enter a num'))
    num_of_guesses = 0
    while not guessed_number:
        if not is_valid_num(guess):
            #return False
            guess = input('i count only digits enter 1<num<100')
            continue
        else:
            num_of_guesses += 1
            break

    guess = int(guess)
    if guess < number:
        print ('entered number is low')
    elif guess > number:
        print ('entered number is high')
    else:
        print ('you got in',num_of_guesses, 'guesses')
        guessed_number = True
main()
@Thanks Issac Full code is below

#Guess the num
import random
def is_valid_num(num):
    if num.isdigit() and 1 <= int(num) <= 100:
        return True
    else:
        return False

def main():
    number = random.randint(1,100)
    guessed_number = False
    guess = input('enter a num')
    #guess = (input('enter a num'))
    num_of_guesses = 0
    while not guessed_number:
        if not is_valid_num(guess):
            #return False
            guess = input('i count only digits enter 1<num<100')
            continue
        else:
            num_of_guesses += 1
            #break

            guess = int(guess)
        if guess < number:
            guess = (input('entered number is low try again'))
        elif guess > number:
            guess = (input('entered number is high try again'))
        else:
            print ('you got in',num_of_guesses, 'guesses')
            guessed_number = True
main()

Output is below Output 如下

>>enter a num55
entered number is high try again55
entered number is high try again45
entered number is high try again88
entered number is high try again30
entered number is high try again10
entered number is low try again20
entered number is low try again25
entered number is high try again22
entered number is low try again23
you got in 10 guesses

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;int&#39;对象没有属性&#39;isdigit&#39; - AttributeError: 'int' object has no attribute 'isdigit' AttributeError: &#39;int&#39; 对象没有属性 &#39;isdigit&#39; 和 NameError - AttributeError: 'int' object has no attribute 'isdigit' and NameError AttributeError: 'int' object 没有属性 'isdigit' 和 arrays - AttributeError: 'int' object has no attribute 'isdigit' with arrays AttributeError:&#39;int&#39;对象没有用户输入的属性&#39;isdigit&#39; - AttributeError: 'int' object has no attribute 'isdigit' from user input AttributeError&#39;int&#39;对象在Ubuntu中的ipython中没有属性isdigit - AttributeError 'int' object has no attribute isdigit in ipython in ubuntu AttributeError: 'generator' object 没有属性 'isdigit' - AttributeError: 'generator' object has no attribute 'isdigit' Python-AttributeError:“ str”对象没有属性“ isDigit” - Python - AttributeError: 'str' object has no attribute 'isDigit' Python AttributeError:“系列”对象没有属性“ isdigit” - Python AttributeError: 'Series' object has no attribute 'isdigit' AttributeError:“列表”对象没有属性“ isdigit” - AttributeError: 'list' object has no attribute 'isdigit' 需要解释 with AttributeError &#39;int&#39; 对象在我的代码中没有属性错误 - need an explanation to the with AttributeError 'int' object has no attribute error in my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM