英文:
python vending machine program- I have two questions
问题
-
我正在创建一个模拟操作的自动贩卖机程序,通过循环实现,当用户输入0时结束循环,但输出中没有打印“输入0取消”的信息。我尝试将其放在所有的if语句之前,但我希望在打印if语句后能够输入一个值。有什么方法可以解决这个问题?
-
当我将 user_val 等于 0 时,它显示 user_val 未定义,但我已经在底部定义了它。
如果有人能帮忙,请!
英文:
-
I'm creating a vending machine program that simulates the action through a loop that ends when the user enters 0 but it doesn't print "0 to cancel" in the output. I tried putting it at the top before the if statements but, I want to be able to enter an input after the if statements are printed. So how could I fix that?
-
It says that user_val is undefined when I equal it to 0 but I did define it at the bottom.
If someone could help please!
print("*********************************")
print("Welcome to Vending Machine Bravo!")
print("*********************************")
print("If you would like to make a selection, please insert the appropriate currency into the machine.")
# Currency deposit [tag:tag-name]
num_5Dollars = 5.00
num_Dollars = 1.00
num_Quarters = .25
num_Dimes = .10
num_Nickels = .05
num_Pennies = .01
currency = [num_5Dollars, num_Dollars, num_Quarters, num_Dimes, num_Nickels, num_Pennies]
if num_5Dollars == 5.00:
print("5.00 for $5 bills")
if num_Dollars == 1.00:
print("1.00 for $1 bills")
if num_Quarters == .25:
print(".25 for Quarters")
if num_Dimes == .10:
print(".10 for dimes")
if num_Nickels == .05:
print(".05 for nickels")
if num_Pennies == .01:
print(".01 for pennies")
if int(float(user_val)) == 0:
print("0 to cancel")
user_val = float(input())
答案1
得分: 1
user_val
在 if int(float(user_val)) == 0
行下定义。
如果你想告诉用户“输入 0 取消”,你不需要检查 int(float(user_val)) == 0
,因为只要这种情况不发生,它就不会打印这个提示。
所以基本上,你需要移除 if int(float(user_val)) == 0:
这一行。
英文:
Your user_val
defined under the line if int(float(user_val)) == 0
.
And if you want to say to user 0 to cancel
, you don't need to check int(float(user_val)) == 0
, because while this doesn't happened, it won't print this instruction.
So basically, you need to remove if int(float(user_val)) == 0:
line
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论