Python 的新手,我的 while 循环计算器有什么问题?

New to Python, what is wrong with my while loop calculator?

我是 Python 的新手,我正在编写一个计算器。它旨在允许用户添加任意​​数量的数字,我正在使用 while 循环并且我拥有它以便 0 结束 while 循环并显示总和。它不会正确计算并提前结束。有人知道为什么吗?这是我的代码:

    import keyboard

    print('Lets add some numbers! Press 0 for total!')
    numb = input()
    totals = float(numb) + float(numb)
    while totals == float(numb) + float(numb):
      numb = input()
    while keyboard.read_key(0):
      print('You pressed the 0 key!')
      break
    print(f'Your total is: {totals}')'''

提前致谢!

您需要在第一个 while 循环中检查 0 输入。此外,您没有将每个输入添加到 totals,您只是在测试 totals 是否已经等于双倍输入。

不需要keyboard,只是测试用户输入的内容。

total = 0
while True:
    numb = float(input("Enter a number, or 0 for the total"))
    if numb == 0:
        break
    total += numb
print(f'Your total is: {total}")