Python虚拟自动售货机

2024-06-16 12:09:58 发布

您现在位置:Python中文网/ 问答频道 /正文

希望有人能帮忙。我正在尝试创建一个虚拟自动售货机。我的代码做得很好,但还是坚持了一些事情。我需要帮助在这个部分创建代码。在

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,所以我有一个想法,这个循环类似于“while TotalCredit<;0.59,但不知道如何发送用户回来添加更多。我列出了下面的代码,这样你就可以看到我走了多远。我只有15岁,只是学习编码,所以请尽可能多的帮助,将不胜感激。在

^{pr2}$

Tags: 代码inputcount硬币事情manyhowint
1条回答
网友
1楼 · 发布于 2024-06-16 12:09:58

把代码的第二部分放到另一个方法中,然后根据用户输入调用vendingMachine(),怎么样?如果我们还可以在下面的条件链中添加一个条件平衡。我们还需要添加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。在

^{pr2}$

现在,程序将继续运行,直到用户在提示时输入“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与{}相同,只是它返回一个字符串(仅在python2.7中)
  • 如果您发现自己不断重复同一行代码,那么您要么需要将其放入函数中,要么为它找到一个更好的位置。例如,在每个if/elif语句之后都使用print ("You have",round(FinalCredit,2),"credit remaning.")。相反,你可以在末尾加上它(我做了)。在

祝你学习编程好运!如果你还有其他问题,请告诉我。以后,请确保在发布之前正确格式化代码。我也会接受@wwii关于伪代码的建议,并事先计划好你要写什么。在

相关问题 更多 >