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():来检查用户输入的内容是否是有效数字.
##
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)