小编Kri*_*Ard的帖子

AttributeError:'int'对象没有属性'isdigit'

numOfYears = 0
cpi = eval(input("Enter the CPI for July 2015: "))
if cpi.isdigit():
    while cpi < (cpi * 2):
        cpi *= 1.025
        numOfYears += 1
    print("Consumer prices will double in " + str(numOfYears) + " years.")
while not cpi.isdigit():
    print("Bad input")
    cpi = input("Enter the CPI for July 2015: ")
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

AttributeError:'int'对象没有属性'isdigit'

由于我是编程新手,我真的不知道它想告诉我什么.我正在使用它if cpi.isdigit():来检查用户输入的内容是否是有效数字.

python

8
推荐指数
3
解决办法
3万
查看次数

我如何摆脱这种无限循环?

##
numOfYears = 0
## Ask user for the CPI
cpi = input("Enter the CPI for July 2015: ")
## If they didn't enter a digit, try again
while not cpi.isdigit():
    print("Bad input")
    cpi = input("Enter the CPI for July 2015: ")
## Convert their number to a float
cpi = float(cpi)
while cpi <= (cpi * 2):
    cpi *= 1.025
    numOfYears += 1
## Display how long it will take the CPI to double
print("Consumer prices will double in …
Run Code Online (Sandbox Code Playgroud)

python infinite-loop while-loop

-1
推荐指数
1
解决办法
264
查看次数

标签 统计

python ×2

infinite-loop ×1

while-loop ×1