gpt4 book ai didi

python - 当我定义中断代码时,为什么我的 while 循环失败了?

转载 作者:行者123 更新时间:2023-11-28 20:36:27 25 4
gpt4 key购买 nike

我是 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 循环都不会中断。关于我哪里出错的任何建议..

最佳答案

您忘记将输入转换为int:

age = int(input())

如果您没有明确地这样做,age 将是一个字符串,并且自然地 '99' != 99

关于python - 当我定义中断代码时,为什么我的 while 循环失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44935814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com