首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在python中使用while循环中的if语句?

在python中使用while循环中的if语句?
EN

Stack Overflow用户
提问于 2018-04-10 03:51:00
回答 1查看 88关注 0票数 0

我有一个自动售货机的问题,用户有三个选择:

  1. 水2.可乐3.佳得乐 水=1美元,可乐=1.5美元,佳得乐=2美元

在从用户那里获取25美分、一角硬币、五分币和便士的输入后,我以美元计算总额。现在,我需要检查总量是否足以满足三种选择的饮料。如果金额低于成本,我需要提示用户再试一次,如果金额更大,则需要为您显示用户更改金额的消息。我相信这需要this循环和if,elif,else语句。以下是我目前的计划:

代码语言:javascript
运行
复制
import sys 
print('You have three drinks to choose from this Vending Machine: \n 1. Water \n 2. Cola \n 3. Gatorade')
water = 1.00
cola = 1.50
gatorade = 2.00
choice = 0
while not choice:
  try:
    choice = int(input('Please select any one of the three choices of drinks (1,2 or 3) from the list above'))
    if choice not in (1,2,3):
        raise ValueError
  except ValueError:
    choice = 0
    print("That is not a valid choice ! Try again")
if choice == 1:
   print("1. water = $1.00")
elif choice == 2:
   print("2. cola = $1.50")
elif choice == 3:
   print("3. gatorade = $2.00")

qrt = int(input("How many quarters did you insert ?"))
dm = int(input("How many dimes did you insert ?"))
nk = int(input("How many nickels did you insert ?"))
pn = int(input("How many pennies did you insert ?"))

total = qrt/4 + dm/10 + nk/20 + pn/100

现在,我需要检查总金额(用户插入的)是否大于或低于他选择的饮料成本。

如果更大,我需要返回更改(显示更改数量的消息)。

如果较小,那么我需要提示用户尝试再次插入硬币。

我怎么能这么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-10 04:50:14

我想这就是你需要做的。同时,根据您的需求,接受逻辑并修改代码。

别就这么抄了。希望能帮上忙。

代码语言:javascript
运行
复制
import sys 
print('You have three drinks to choose from this Vending Machine: \n 1. Water \n 2. Cola \n 3. Gatorade')
water = 1.00
cola = 1.50
gatorade = 2.00
choice = 0
while not choice:
    try:
        choice = int(input('Please select any one of the three choices of drinks (1,2 or 3) from the list above: '))
        if choice not in (1,2,3):
            raise ValueError
    except ValueError:
        choice = 0
        print("That is not a valid choice ! Try again")

if choice == 1:
    print("1. water = $1.00")
    total_to_be = 1.00
elif choice == 2:
    print("2. cola = $1.50")
    total_to_be = 1.50
elif choice == 3:
    print("3. gatorade = $2.00")
    total_to_be = 2.00

qrt = int(input("How many quarters did you insert ?"))
dm = int(input("How many dimes did you insert ?"))
nk = int(input("How many nickels did you insert ?"))
pn = int(input("How many pennies did you insert ?"))

total = qrt/4 + dm/10 + nk/20 + pn/100

# total = round(total, 2)

while total != total_to_be: 

    if total >= total_to_be:
        print(total)
        break

    else:
        print("======================= Please try Again ================================")
        qrt = int(input("How many quarters did you insert ?"))
        dm = int(input("How many dimes did you insert ?"))
        nk = int(input("How many nickels did you insert ?"))
        pn = int(input("How many pennies did you insert ?"))

        total = qrt/4 + dm/10 + nk/20 + pn/100
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49745012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档