我有一个自动售货机问题,为用户提供3种选择:
水2.可乐3.佳得乐
水= $ 1.00,可乐= $ 1.50,gatorade = $ 2.00
在输入用户的四分之一,硬币,镍币和硬币后,我用美元计算总金额。现在我需要检查总量是否足以满足3种饮料中的每一种。如果金额低于成本,我需要提示用户再试一次,如果金额大,那么我需要为用户的更改金额显示一条消息。我相信这需要while循环和if,elif,else语句。以下是我目前的计划:
create-react-app
现在,我需要检查总金额(用户插入的金额)是大于还是小于他所选饮料的成本。
如果更大,我需要返回更改(显示更改金额的消息)。
如果较小,那么我需要提示用户再次尝试插入硬币。
我该怎么做?
答案 0 :(得分:1)
我认为这是你需要做的。还可以根据您的要求采用逻辑并修改代码。
不要复制它。希望它有所帮助。
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