能够在Python的ifelse语句中执行多个操作?

2024-06-25 23:53:52 发布

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

好的,我正在用Python创建一个收银机命令行程序。我希望该程序能够告诉多少二十,十,五,一,四分之一,一角硬币,五分镍币和便士应给予改变。我知道什么方程需要写下来,我可以想出如何打印出20年代,但我似乎不知道从那里去哪里。例如,我的代码如下所示:

    if change >= 2000:
      twenties = change / 2000
      moneyform = "%i" % twenties
      print("Twenties "), moneyform
    else:
      print("Twenties: 0")
    next = change - 2000
    if next >= 1000:
      tens = next / 1000
      moneyformtwo = "%i" % tens
      print("Tens "), moneyformtwo
    else:
      print("Tens: 0")

以此类推。你知道吗

现在我知道我做的不对,但是发生的是20的打印是正确的,但是程序停止了,没有继续到10。我该怎么做才能解决这个问题?你知道吗


Tags: 命令行程序if硬币changeelsenextprint
1条回答
网友
1楼 · 发布于 2024-06-25 23:53:52

为什么还要使用if/else语句?你知道吗

twenties = int(change / 2000)
change = change % 2000

tens = int(change / 1000)
change = change % 1000

fives = int(change / 500)
change = change % 500

最后打印结果

编辑:我在回答中做了同样的假设,整数代表美分,正如你在问题陈述中所做的那样。在顶部试试这个:

amount_paid = 50.00
price = 24.50
change = amount_paid*100 - price*100

相关问题 更多 >