如何循环直到一个特定值或通过输入再次请求

时间:2014-04-30 20:10:24

标签: python loops if-statement input break

我有所有必要的代码,以便在有人按下客户或产品价值时执行必要的操作。我要问的是帮助我找到一种方法来重复询问值作为输入的问题它不是三个选择中的一个(客户或产品或q)。

此外,如何重复整个过程,直到给出的值为q

最后如果它给出值q,如何停止progr。

该计划应该是这样的:

  

欢迎来到全球杂货店,世界领导人在那里购物!

     

查看每个客户类型“客户”的销售信息   查看每种产品类型“产品”的销售信息   退出键入'q'。
  客户

     

请选择“客户”,“产品”或“q”退出   项目

     

请选择“客户”,“产品”或“q”退出   客户

     

barakobama
  ----------
  谷类:9.21薯片:1.09马铃薯:2.67欧芹:0.76糖:1.98

     

vladimirputin
  -------------
  面包:0.66欧芹:1.33牛奶:2.87

     

请选择“客户”,“产品”或“q”退出   q

     

感谢您的光临,再来一次!

到现在为止我只有这个:

print("Welcome to the Global Grocery, where world leaders do their shopping!","\n") 

print("To view sales information for each customer type \'customers\'.") 
print("To view sales information for each product type \'products\'.")
print("To quit type \'q\'.")
data=input("")

while data!="customers" or data!="products" or data!="q":
    print("Please choose \'customers\', \'products'', or \'q\' to quit.")
    data=input("")

    if data=="customers":

    elif data="products":

    else:
         print("Thank you for visiting, come again!")
         break   

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

#!/usr/bin/env python3

print("Welcome to the Global Grocery, where world leaders do their shopping!","\n")

print("To view sales information for each customer type \'customers\'.")
print("To view sales information for each product type \'products\'.")
print("To quit type \'q\'.")

while True:  # Keep going forever until a 'break' statement is met
    data = input("")
    if data == "q":
        print("Good bye!")
        break  # end the 'while' loop
    elif data == "customers":
        print("Ok, customer stuff")
    elif data == "products":
        print("Ok, product stuff")
    else:  # A non-valid option must have been selected
        print("Please choose \'customers\', \'products'', or \'q\' to quit.")