gpt4 book ai didi

python - 如何在 python 中退出 while 循环?

转载 作者:行者123 更新时间:2023-11-28 19:54:38 24 4
gpt4 key购买 nike

我正在处理这个需要使用 2 个 while 循环的简单任务。第一个 while 循环检查小时数是否小于 0,如果是,则循环应继续询问用户。这是我的代码:

hours = float(input('Enter the hours worked this week: '))

count = 0
while (0 > hours):
print ('Enter the hours worked this week: ')
count = count + 1

pay = float(input('Enter the hourly pay rate: '))
while (0 > pay):
print ('Enter the hourly pay rate: ')
count = count + 1

total_pay = hours * pay

print('Total pay: $', format(total_pay, ',.2f'))

最佳答案

break 是您要查找的内容。

x = 100
while(True):
if x <= 0:
break
x -= 1
print x # => 0

至于您的示例,似乎没有什么会导致中断发生。例如:

hours = float(input('Enter the hours worked this week: '))

count = 0

while (0 > hours):
print ('Enter the hours worked this week: ')
count = count + 1

您根本没有编辑 hours 变量。这只会继续打印出 "Enter the hours worked this week: " 并无限递增 count。我们需要知道提供更多帮助的目标是什么。

关于python - 如何在 python 中退出 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330197/

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