我是一名新程序员,我正在尝试让这段代码正常工作,但我似乎无法绑定变量 x、y 和 z

问题描述 投票:0回答:1
price_apple = 1.00
price_pear = 1.50
apple_stock = 50
pear_stock = 30
totalstock = apple_stock + pear_stock
while totalstock != 0:
  totalstock = apple_stock + pear_stock
  if totalstock == 0:
    print("Sadly we don't have any more fruit in stock.")
    break
  order = input("What would you like to order? ")
  if order == "apples":
    x, y, z = apple_stock, price_apple, "apples"      
  elif order == "pears":
    x, y, z = pear_stock, price_pear, "pears" 
  if order == "apples" or order == "pears" and totalstock > 0:
    number = list(map(int,input("How many would you like to order? ").strip().split()))
    for individual_order in number:
      if x >= individual_order:
        x = x - individual_order
        print(order, )
        print("You have enough", z, "in stock")
        print("Your total price is", str(order * x))
        print("Your", z, "stock is now", x, z)
      else:
        break
  if order != "apples" and order != "pears":*
    break

我希望 if 或 elif 继续执行下一行代码,但变量未绑定。

这是我从原始代码中得到的错误消息:

Traceback (most recent call last):
    File "/home/runner/School-project/mail.py", line 134, in <module>
        if order == "apples" and x == 0:
NameError: name 'x' is not defined

我尝试将代码放在这样的变量下,它可以工作,但我不想要巨大的代码(这是我代码的一小部分和简化的部分)这也会使变量变得无用。

  #code
  order = input("What would you like to order? ")
  if order == "apples":
    x, y, z = apple_stock, price_apple, "apples"
    if order == "apples" and totalstock > 0:
      number = list(map(int,input("How many would you like to order? ").strip().split()))
      for individual_order in number:
        if x >= individual_order:
          x = x - individual_order
          print(order, )
          print("You have enough", z, "in stock")
          print("Your total price is", str(order * x))
          print("Your", z, "stock is now", x, z)
  elif order == "pears":
    x, y, z = pear_stock, price_pear, "pears"
    number = list(map(int,input("How many would you like to order? ").strip().split()))
    for individual_order in number:
      if x >= individual_order:
        x = x - individual_order
        print(order, )
        print("You have enough", z, "in stock")
        print("Your total price is", str(order * x))
        print("Your", z, "stock is now", x, z)

如果能解决这个问题就太好了,我会把接近 900 行增加到大约 250 行

python if-statement variables
1个回答
0
投票

这是我为您编写的一个小演示,请尝试更好地构建您的代码。

这是一个可循环的代码,它也将状态保存在内存中,它是高度动态的

inventory = {
    'apple': {
        'total': 50,
        'price': 1.00
    },
    'pear': {
        'total': 30,
        'price': 1.50
    },
}

currentItem = ""
currentAmount = 0

while True:
    print("======================================")
    print("==              Menu                ==")
    print("======================================")
    for key in inventory.keys():
        line = "==  {name} -> {items_total} @ ${price} each".format(name=key, items_total = inventory[key]['total'], price = inventory[key]['price'])
        print(line + ( " " * (36 - len(line))) + "==")
    print("======================================")
    currentItem = input("What would you like to order? ")
    if currentItem not in inventory.keys():
        print("Invalid Item Selected")
        continue
    currentAmount = int(input("How many would you like? "))
    if currentAmount <= 0:
        print("Invalid Amount Entered, Value to low")
        continue
    if currentAmount > inventory[currentItem]['total']:
        print("Invalid Amount Entered, Value to high")
        continue
    inventory[currentItem]['total'] = inventory[currentItem]['total'] - currentAmount
    purchasedPriceAmount = inventory[currentItem]['price'] * currentAmount
    print("You have purchased {amount} {name}s for ${price} each".format(amount=currentAmount, name=currentItem, price=inventory[currentItem]['price']))
    print("With a total of ${totalPrice}".format(totalPrice=purchasedPriceAmount))

运行示例

Programiz Python 在线编译器

测试过代码

© www.soinside.com 2019 - 2024. All rights reserved.