gpt4 book ai didi

python - 当用户只点击进入时,我试图停止循环?

转载 作者:行者123 更新时间:2023-12-04 10:39:33 26 4
gpt4 key购买 nike

当用户输入一个数字 <=0 或用户只点击输入时,我试图停止这个循环。
当用户只点击进入时,我如何创建中断?

    number = int(input('Type a number, [to stop type 0 or less]'))
num_sum = number
times = 0

while True:
number = int(input('Type a number, [to stop type 0 or less]'))
num_sum += number
times += 1
if number <= 0:
break

average = num_sum / times
print(f'{times} number received \nThe average of number recceived is:'+'{:.4f}'.format(average))

最佳答案

调用 input()int()作为两个独立的步骤。

while True:
# call input() by itself, without calling int()
answer = input('Type a number, [to stop type 0 or less]')

# if the user pressed enter without typing an answer, break
if not answer:
break

# otherwise convert answer to an integer
number = int(answer)

# remaining code is unchanged

关于python - 当用户只点击进入时,我试图停止循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59994760/

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