我有一个自动售货机的问题,用户有三个选择:
在从用户那里获取25美分、一角硬币、五分币和便士的输入后,我以美元计算总额。现在,我需要检查总量是否足以满足三种选择的饮料。如果金额低于成本,我需要提示用户再试一次,如果金额更大,则需要为您显示用户更改金额的消息。我相信这需要this循环和if,elif,else语句。以下是我目前的计划:
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现在,我需要检查总金额(用户插入的)是否大于或低于他选择的饮料成本。
如果更大,我需要返回更改(显示更改数量的消息)。
如果较小,那么我需要提示用户尝试再次插入硬币。
我怎么能这么做?
发布于 2018-04-10 04:50:14
我想这就是你需要做的。同时,根据您的需求,接受逻辑并修改代码。
别就这么抄了。希望能帮上忙。
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/100https://stackoverflow.com/questions/49745012
复制相似问题