Python if/else以特定的方式运行,但如果用户输入高于特定的数字,则在第一个if/else之后停止

2024-06-06 21:40:51 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我是python新手,我写这篇文章是为了分配任务,例如,如果我运行101作为输入,它将正确运行并显示我需要“1$100账单和1$1账单”,但如果我运行我需要125美元,它将作为“1$100账单和1$20账单”执行,但不执行剩余的5美元账单。我还意识到,我不能运行任何低于100,但我可以修复。我试图理解If/else语句

#
# Python ATM machine
#

import math
withdrawl = int(input("how much money do you want to withdrawl (max withdrawl is $200): "))

if (withdrawl > 200):
    print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
    print("1 100 Dollar Bill")
    remainder1 = withdrawl - 100
    if ((remainder1 / 20) >= 1):
        print (math.trunc(remainder1/20)," 20 Dollar Bills")
    else:
        print("0 20 Dollar Bills")
        remainder2 = remainder1 - (math.trunc(remainder1/20)*20)
        if ((remainder2 / 10) >= 1):
            print (math.trunc(remainder2/10)," 10 dollar Bills")
        else:
            print ("0 10 Dollar Bills")
            remainder3 = remainder2 - (math.trunc(remainder2/10)*10)
            if ((remainder3 / 5) >= 1):
            print (math.trunc(remainder3 / 5)," 5 dollar Bills")
            else:
                print("0 5 dollar bills")
                remainder4 = remainder3 - (math.trunc(remainder3/5)*5)
                if ((remainder4 / 1) >= 1):
                    print (math.trunc(remainder3/1)," 1 dollar Bills")
                else:
                    print("Thank you for using our ATM machine")

print ("Thank you for using our ATM machine")

Tags: ifmathmachineelse账单printdollarbills
2条回答

尝试使用while循环,如下所示:

withdraw = int(input("how much money do you want to withdraw (max withdraw is $200): "))

data = []
if (withdraw > 200):
    print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
    rest = withdraw
    while rest > 0:
        if rest > 100:
            data.append(100)
            rest = rest - 100
        if 100 > rest >= 50:
            data.append(50)
            rest = rest - 50
        if 50 > rest >= 20:
            data.append(20)
            rest = rest - 20
        if 20 > rest >= 10:
            data.append(10)
            rest = rest - 10
        if 10 > rest >= 5:
            data.append(5)
            rest = rest - 5
        if 5 > rest > 0:
            data.append(1)
            rest = rest - 1

37的输出:

[20, 10, 5, 1, 1]
import math
withdrawl = int(input("how much money do you want to withdrawl (max withdrawl is $200): "))

if (withdrawl > 200):
    print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
    if(withdrawl >= 100):
        print("1 100 Dollar Bill")
        remainder = withdrawl - 100
    else:
        remainder = withdrawl
    if ((remainder / 20) >= 1):
        print (math.trunc(remainder/20)," 20 Dollar Bills")
        remainder = remainder - (math.trunc(remainder/20)*20)
    else:
        print("0 20 Dollar Bills")
    if ((remainder / 10) >= 1):
        print (math.trunc(remainder/10)," 10 dollar Bills")
        remainder = remainder - (math.trunc(remainder/10)*10)
    else:
        print ("0 10 Dollar Bills")
    if ((remainder / 5) >= 1):
        print (math.trunc(remainder / 5)," 5 dollar Bills")
        remainder = remainder - (math.trunc(remainder/5)*5)

    else:
        print("0 5 dollar bills")
    if (remainder != 0):
        print (remainder," 1 dollar Bills")
print ("Thank you for using our ATM machine")

因此,您忘记了更新余数的值+您不必使用其他变量作为余数,只要一个就足够了,这段代码应该适合您。 正如@Arthurtaca所说的,每个if/else块都会发生,不管前一个块中发生了什么

相关问题 更多 >