H3lp高中生使用Python循环

H3lp高中生使用Python循环,python,loops,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")

这应该是商店中一个系统的代码,如果有人想买别的东西,它会一次又一次地循环。请帮忙

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-谢谢。

问题修复和循环优化-

您遇到的问题是输入函数周围的额外括号

循环现在被优化了,因为你的整个购买周期现在都在这个while True:loop中。它将一次又一次地重复,直到用户输入不同的内容,然后在另一个问题上输入yes

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

最后一部分基本上没有任何用处,即使它有效。我建议你使用另一个答案中的代码。

试着去掉外括号:other=input你还想买什么吗?你想把你要求输入的部分放在一个循环中,基本上是整个事情,除了你声明变量的地方。这对我来说很困惑-我建议你把整个购买周期放在一个循环中当别人的答案不是肯定的时候,我们通常不喜欢在这里修改别人的作业。至少多给点动力和解释。需要发生的事情,没有发生的事情,调试的方式,等等。而且,H3lp也不是真正为你的事业工作-由于这显然是一个学生要求修复,因此可以解释一下他们的原始代码中出现了什么错误、修复了什么以及如何优化。
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")