擅长:python、mysql、java
<pre><code>numOfYears = 0
# since it's just suppposed to be a number, don't use eval!
# It's a security risk
# Simply cast it to a string
cpi = str(input("Enter the CPI for July 2015: "))
# keep going until you know it's a digit
while not cpi.isdigit():
print("Bad input")
cpi = input("Enter the CPI for July 2015: ")
# now that you know it's a digit, make it a float
cpi = float(cpi)
while cpi < (cpi * 2):
cpi *= 1.025
numOfYears += 1
# it's also easier to format the string
print("Consumer prices will double in {} years.".format(numOfYears))
</code></pre>