H3lp高中生与Python循环

时间:2018-09-12 13:39:23

标签: python loops

这应该是商店中系统的代码,如果有人要购买其他产品,该系统会一次又一次地循环。请帮忙。

menu=["apple","water","juice"]
apple=50
water=80
juice=100


money=int(input("How much money in pennies do you have?"))
if money>=100:
  print("We have the following items you can buy: apple, water, juice")
elif money>=80 and money<=100:
  print("We have the following items you can buy: apple, water")
elif money>=50 and money<=80:
  print("We have the following item you can buy: apple")
else:
  print("Sorry, you can't buy anything.")


buy=input("What do you want to buy?")
if buy=="apple":
  print("You have",money-50)
elif buy=="water":
  print("You have",money-80)
else:
  print("You have",money-100)

other=(input("Do you want to buy anything else?"))
if other=="yes":
  while x=0:
  print(x)
    continue
elif other=="no":
  x+1
else:
  print("Error")

最后一部分不起作用-有人可以修复它吗?这是Python 3-谢谢。

2 个答案:

答案 0 :(得分:0)

已解决问题并已优化循环-

您遇到的问题是输入功能周围的括号()

由于您的整个购买周期现在都在此while True:循环中,因此该循环已得到优化。它会一次又一次地重复,直到用户在另一个问题上输入与“是”不同的内容为止。

while True: 
    menu=["apple","water","juice"]
    apple=50
    water=80
    juice=100


    money=int(input("How much money in pennies do you have?"))
    if money>=100:
        print("We have the following items you can buy: apple, water, juice")
    elif money>=80 and money<=100:
        print("We have the following items you can buy: apple, water")
    elif money>=50 and money<=80:
        print("We have the following item you can buy: apple")
    else:
        print("Sorry, you can't buy anything.")


    buy=input("What do you want to buy?")
    if buy=="apple":
        print("You have",money-50)
    elif buy=="water":
        print("You have",money-80)
    else:
        print("You have",money-100)

    other=input("Do you want to buy anything else?")
    if other=="yes":
        continue
    else:
        break

答案 1 :(得分:0)

other=(input("Do you want to buy anything else?"))
if other=="yes":
   while x=0:        <-- should use x==0, but x is not declared
   print(x)          <-- wrong indentation
       continue
elif other=="no":
    x+1              <-- even if x was declared, this only sum and nothing 
else:                    more (nothing changes in your program)
    print("Error")

最后一部分即使有效,也基本上没有任何用处。我建议您使用其他答案中发布的代码。

相关问题