count = 0
TotalCredit = 0
coinNum = int (input("How many coins would you like to enter: "))
while count in range (coinNum):
coin = float (input ("Coins accepted 0.10 0.20 0.50 1.00: £ "))
TotalCredit = TotalCredit + coin
count = count + 1
因此,如果输入的硬币不是0.10 0.20 0.50 1.00,则会打印一条消息"输入的无效硬币请再试一次" 并循环回来开始。
我还需要一个while循环,所以如果输入的信用不足,就会打印出来 "资金不足请加入更多信用"并返回允许您添加信用。我知道最低信用额度是0.59,所以我知道循环是类似于'而TotalCredit< 0.59但不知道如何发送用户回来添加更多。我已经列出了下面的代码,所以你可以看到我走了多远。我只有15岁,只是学习编码所以请尽可能多的帮助将非常感激。
def vendingMachine():
count = 0
TotalCredit = 0
coinNum = int (input("How many coins would you like to enter: "))
while count in range (coinNum):
coin = float (input ("Coins accepted 0.10 0.20 0.50 1.00: £ "))
TotalCredit = TotalCredit + coin
count = count + 1
print ("You have £",round(TotalCredit,2),"credit " )
print ("")
print ("Choose your item:")
print ("")
print ("1.Coco-Cola")
print ("2.Walkers Cheese & Onion")
print ("3.Lucozade")
print ("4.Wosits")
print ("5.Water")
print ("")
FinalCredit = TotalCredit
round (FinalCredit, 2)
item = int (input ("Enter the number for your item: "))
print ("")
while item <1 or item >5:
print ("This item is not available.")
item = int (input ("Enter the number for your item: "))
if item == 1:
FinalCredit = TotalCredit - 0.59
print ("You now have a Coca-Cola can, costing £0.59.")
print ("You have",round(FinalCredit,2),"credit remaning.")
elif item == 2:
FinalCredit = TotalCredit - 0.69
print ("You now have a Walkers crisp packet, costing £0.69.")
print ("You have", round(FinalCredit,2),"credit remaning.")
elif item == 3:
FinalCredit = TotalCredit - 0.99
print ("You now have a Lucozade drink, costing £0.99.")
print ("You have" ,round(FinalCredit,2),"credit remaning.")
elif item == 4:
FinalCredit = TotalCredit - 0.59
print ("You now have a Wosits crisp packet, costing £0.59.")
print ("You have",round(FinalCredit,2),"credit remaning.")
elif item == 5:
FinalCredit = TotalCredit - 0.79
print ("You now have a bottle of water, costing £0.79.")
print ("You have",round(FinalCredit,2),"credit remaning.")
else:
print ("This not an option.")
print ("")
print ("The rest of your money, £(0), has been
outputted.".format(round(FinalCredit,2)))
vendingMachine ()
答案 0 :(得分:0)
如何将代码的第二部分放在不同的方法中,然后根据用户输入调用vendingMachine()
?此外,为了阻止余额低于0,我们可以在if / elif链中添加额外的条件语句。我们还需要添加TotalCredit
参数(如下所述)。我添加了一些其他的编辑,我稍后会详细介绍。
def query(TotalCredit):
print ("You have £" + str(round(TotalCredit,2)) + " credit\n")
print ("Choose your item:\n\n1.Coco-Cola\n2.Walkers Cheese & Onion\n3.Lucozade\n4.Wosits\n5.Water\n")
FinalCredit = TotalCredit
round (FinalCredit, 2)
item = int (input ("Enter the number for your item: "))
print ("")
while item <1 or item >5:
print ("This item is not available.")
item = int (input ("Enter the number for your item: "))
if item == 1 and FinalCredit >= 0.59:
FinalCredit = TotalCredit - 0.59
print ("You now have a Coca-Cola can, costing £0.59.")
elif item == 2 and FinalCredit >= 0.69:
FinalCredit = TotalCredit - 0.69
print ("You now have a Walkers crisp packet, costing £0.69.")
elif item == 3 and FinalCredit >= 0.99:
FinalCredit = TotalCredit - 0.99
print ("You now have a Lucozade drink, costing £0.99.")
elif item == 4 and FinalCredit >= 0.59:
FinalCredit = TotalCredit - 0.59
print ("You now have a Wosits crisp packet, costing £0.59.")
elif item == 5 and FinalCredit >= 0.79:
FinalCredit = TotalCredit - 0.79
print ("You now have a bottle of water, costing £0.79.")
else:
print ("This not an option.\n")
print ("The rest of your money, £(0), has been outputted.".format(round(FinalCredit,2)))
print ("You have "+str(round(FinalCredit,2))+" credit remaning.")
again = input("Would you like to enter the vending machine (y/n)?")
while again != 'y' and again != 'n':
again = input("Please input y or n")
if again == 'y':
vendingMachine()
接下来,在vendingMachine()
中,我们需要做的就是拨打query()
并传递TotalCredit
。
def vendingMachine():
TotalCredit = 0
count = 0
coinNum = int (input("How many coins would you like to enter: "))
while count in range (coinNum):
coin = float (input ("Coins accepted 0.10 0.20 0.50 1.00: £ "))
TotalCredit = TotalCredit + coin
count = count + 1
query(TotalCredit)
现在,程序将继续运行,直到用户在出现提示时输入“n”。这是完整的代码:
def query(TotalCredit):
print ("You have £" + str(round(TotalCredit,2)) + " credit\n")
print ("Choose your item:\n\n1.Coco-Cola\n2.Walkers Cheese & Onion\n3.Lucozade\n4.Wosits\n5.Water\n")
FinalCredit = TotalCredit
round (FinalCredit, 2)
item = int (input ("Enter the number for your item:\n"))
while item <1 or item >5:
print ("This item is not available.")
item = int (input ("Enter the number for your item: "))
if item == 1 and FinalCredit >= 0.59:
FinalCredit = TotalCredit - 0.59
print ("You now have a Coca-Cola can, costing £0.59.")
elif item == 2 and FinalCredit >= 0.69:
FinalCredit = TotalCredit - 0.69
print ("You now have a Walkers crisp packet, costing £0.69.")
elif item == 3 and FinalCredit >= 0.99:
FinalCredit = TotalCredit - 0.99
print ("You now have a Lucozade drink, costing £0.99.")
elif item == 4 and FinalCredit >= 0.59:
FinalCredit = TotalCredit - 0.59
print ("You now have a Wosits crisp packet, costing £0.59.")
elif item == 5 and FinalCredit >= 0.79:
FinalCredit = TotalCredit - 0.79
print ("You now have a bottle of water, costing £0.79.")
else:
print ("This not an option.\n")
print ("The rest of your money, £(0), has been outputted.".format(round(FinalCredit,2)))
print ("You have " + str(round(FinalCredit,2)) + " credit remaning.")
again = input("Would you like to enter the vending machine (y/n)?\n")
while again != 'y' and again != 'n':
again = input("Please input y or n\n")
if again == 'y':
vendingMachine()
def vendingMachine():
count = 0
TotalCredit = 0
coinNum = int (input("How many coins would you like to enter: "))
while count in range (coinNum):
coin = float (input ("Coins accepted 0.10 0.20 0.50 1.00: £ "))
TotalCredit = TotalCredit + coin
count = count + 1
query(TotalCredit)
vendingMachine()
其他一些事情:
\n
放在字符串中以添加新行,而不是使用emtpy print语句str
将字符串转换为字符串,字符串之间的+
连接起来,或将它们组合在一起。raw_input
与input
相同,除了它返回一个字符串(仅在Python 2.7中)print ("You have",round(FinalCredit,2),"credit remaning.")
。相反,你可以在最后添加它(我做了)。 祝学习编程好运!如果您有任何其他问题,请告诉我。将来,请确保在发布之前正确格式化代码。我也会接受@wwii关于伪代码的建议并预先计划你要写的内容。