python我不明白为什么我的循环在条件不再成立后就结束

问题描述 投票:0回答:1

enter image description here

正如我在标题中提到的,循环结束后会继续。我在网上寻找任何修复程序,但没有找到很多。我是一个完全的初学者,三天前才开始编码,所以请善意地帮助我。

python computer-science
1个回答
0
投票

我已经尝试过你的代码,我认为你的编译器有问题。 我运行了代码并且运行成功。我尝试了你的输入。

在另一个编译器中尝试您的代码。

我尝试过的代码:

def main():
    amount_due = 50
    print (f"Amount due : {amount_due}")
    inserted_coin = int(input("Inserted coin : "))
    while amount_due != 0:
        if amount_due == 0 : break
        if inserted_coin == 25 :
            amount_due = amount_due - 25
            print(f"Amount due : {amount_due}")
            inserted_coin = int(input("Inserted coin : "))
        elif inserted_coin == 10 :
            amount_due = amount_due - 10
            print(f"Amount due : {amount_due}")
            inserted_coin = int (input ("Inserted coin : "))
        elif inserted_coin == 5 :
            amount_due = amount_due - 5
            print(f"Amount due : {amount_due}")
            inserted_coin = int(input ("Inserted coin:"))
        else:
            print(f"Amount due : {amount_due}")
            inserted_coin = int(input("Inserted coin : "))
    print (f"Owed change: {amount_due - inserted_coin}")
main()

输出:

Amount due : 50
Inserted coin : 25
Amount due : 25
Inserted coin : 25
Amount due : 0
Inserted coin : 25
Owed change: -25
© www.soinside.com 2019 - 2024. All rights reserved.