我是Python新手,正在探索一个简单的辅助项目,以了解函数和循环的工作原理。
我这里有这个代码
age = 0
def ageInMonths (age):
months = int(age) * 12
return print('You are ' + str(months) + ' months old!')
def ageInDays(age):
days = int(age)*365
return print('You are ' + str(days) + ' days old!')
def ageInHrs(age):
hrs = int(age)* 8765.81
return print('You are ' + str(hrs) + ' hours old!')
def ageinMin(age):
minutes = int(age)* 525948.8
return print('You are ' + str(minutes) + ' minutes old!')
def ageinSec(age):
sec= int(age)* 31556926
return print('You are ' + str(sec) + ' seconds old!')
def agePrint(age):
age=age
ageInMonths(age)
ageInDays(age)
ageInHrs(age)
ageinMin(age)
ageinSec(age)
print('Done')
return
while age != 99:
print( 'Please enter your age?')
age = input()
if age == 99:
break
print('You are ' + age + ' years old!')
agePrint(age)
每次输入99时,while循环都不会中断。对我出错的地方有任何建议..
答案 0 :(得分:7)
您忘记将输入转换为int
:
age = int(input())
如果您没有明确地这样做,age
将是一个字符串,自然'99' != 99
。