【问题标题】:Python While Loop program CSEPython While 循环程序 CSE
【发布时间】:2020-09-28 22:04:28
【问题描述】:

我有一个需要完成的程序,但我不知道如何完成。我需要让它使用一个while循环并询问他们是否想订购,一旦他们完成了他们是否想再次订购,然后返回到主代码块。我一点也不擅长这个。这是我现在所拥有的:(编辑:对截图感到抱歉)

order= input('Hi, would you like to order anything? Yes (y), No (n)')
if order == "n":
    print("Ok have a nice day")
elif order == "y":
    ordering_y = type_of_sandwich = input('What kind of sandwich would you like? Chicken for $5.25 (c), Tofu for $5.75 (t), Beef for $6.25(b)?')
if type_of_sandwich == "c":
  print('You chose the chicken sandwich for $5.25')
elif type_of_sandwich == "t":
  print ('You chose a tofu for $5.75 sandwich')
elif type_of_sandwich == "b":
  print('You chose a beef sandwich for $6.25')
bev = input("Would you like a beverage? Yes (y), No (n)?")
if bev == "n":
    print("Ok, thanks for your business, would you like to order again?")
elif bev == "y":
  size= input('Small for $1.00 (s), Medium for $1.75 (m), Large for $2.25 (l)?')
if size == "s":
        print("You ordered a small beverage for $1.00, would you like to order again?")
elif size == "m":
        print("You ordered a Medium beverage for $1.75, would you like to order again?")
elif size == "l":
        print("You ordered a large beverage for $2.25, would you like to order again?")

order_again= input('Would you like to order again? Yes (y), No (n)')
while true:
    if order_again == "n":
        print("Ok, thanks for your business.")
        break
    elif order_again =="y":
        repeat order

【问题讨论】:

  • 请将您的代码作为文本粘贴到此处,而不是屏幕截图。
  • 欢迎来到 Stack Overflow。为了进一步帮助您,我们需要更多详细信息。请花一些时间阅读How to Askminimal reproducible example,了解如何改进您的问题,以便我们提供您正在寻找的答案。
  • 请不要发布代码、数据或 Tracebacks 的图像。将其复制并粘贴为文本,然后将其格式化为代码(选择它并输入ctrl-k)...Discourage screenshots of code and/or errors
  • while True: runCode() ... done = input('Are you finished?') ... if done = 'Yes': break.... else: continue ...因此,从您的代码的外观来看,您只需要另一个 while True 循环。

标签: python loops while-loop iteration


【解决方案1】:

您应该在用户输入“n”后添加break。它打破循环并退出while。

【讨论】:

    【解决方案2】:

    这是你的代码检查它:

    def order():
        order= input('Hi, would you like to order anything? Yes (y), No (n)')
    
        if order == "n":
            print("Ok have a nice day")
            return False
        elif order == "y":
            ordering_y = type_of_sandwich = input('What kind of sandwich would you like? Chicken for $5.25 (c), Tofu for $5.75 (t), Beef for $6.25(b)?')
        if type_of_sandwich == "c":
          print('You chose the chicken sandwich for $5.25')
        elif type_of_sandwich == "t":
          print ('You chose a tofu for $5.75 sandwich')
        elif type_of_sandwich == "b":
          print('You chose a beef sandwich for $6.25')
        bev = input("Would you like a beverage? Yes (y), No (n)?")
        if bev == "n":
            print("Ok, thanks for your business, would you like to order again?")
        elif bev == "y":
          size= input('Small for $1.00 (s), Medium for $1.75 (m), Large for $2.25 (l)?')
        if size == "s":
                print("You ordered a small beverage for $1.00, would you like to order again?")
        elif size == "m":
                print("You ordered a Medium beverage for $1.75, would you like to order again?")
        elif size == "l":
                print("You ordered a large beverage for $2.25, would you like to order again?")
        return True
    
    
    if __name__ == "__main__":
        if order():
            order_again = input('Would you like to order again? Yes (y), No (n)')
            if order_again == "n":
                print("Ok, thanks for your business.")
            elif order_again =="y":
               order()
    

    【讨论】:

    • 非常感谢,正是我想要的。 :)
    • 将答案按钮设为绿色
    【解决方案3】:

    谢谢,我不确定我这样做是否正确,但现在它在第 7 行给了我一个语法错误。

    while True:
        order= input('Hi, Would you like to order anything? Yes (y), No (n)')
        if order=="n":
            print("ok have a nice day")
            break
        elif order=="y":
     ordering = type_of_sandwich = input('What kind of sandwich would you like? Chicken for $5.25 (c), Tofu for $5.75 (t), Beef for $6.25(b)?')
    if type_of_sandwich == "c":
      print('You chose the chicken sandwich for $5.25')
    elif type_of_sandwich == "t":
      print ('You chose a tofu for $5.75 sandwich')
    elif type_of_sandwich == "b":
      print('You chose a beef sandwich for $6.25')
    bev = input("Would you like a beverage? Yes (y), No (n)?")
    if bev == "n":
        print("Ok, thanks for your business.")
    elif bev == "y":
      size= input('Small for $1.00 (s), Medium for $1.75 (m), Large for $2.25 (l)?')
    if size == "s":
            print("You ordered a small beverage for $1.00.")
    elif size == "m":
            print("You ordered a Medium beverage for $1.75.")
    elif size == "l":
            print("You ordered a large beverage for $2.25.")
    

    【讨论】:

    • 您的代码中有一些非常时髦的缩进。此外,这是答案部分,您填写了答案。您可以评论某人的答案。这个网站的格式不像博客。
    • 对不起,我刚刚注册了我的帐户。我在这方面遇到了很多麻烦(这是一项学校作业)。我会记住这一点。
    【解决方案4】:
    def get_order():
        ordering_y = type_of_sandwich = input('What kind of sandwich would you like? Chicken for $5.25 (c), Tofu for $5.75 (t), Beef for $6.25(b)?')
        if type_of_sandwich == "c":
                print('You chose the chicken sandwich for $5.25')
        elif type_of_sandwich == "t":
                print ('You chose a tofu for $5.75 sandwich')
        elif type_of_sandwich == "b":
                print('You chose a beef sandwich for $6.25')
        bev = input("Would you like a beverage? Yes (y), No (n)?")
        if bev == "n":
            print("Ok, thanks for your business, would you like to order again?")
        elif bev == "y":
            size= input('Small for $1.00 (s), Medium for $1.75 (m), Large for $2.25 (l)?')
            if size == "s":
                print("You ordered a small beverage for $1.00, would you like to order again?")
            elif size == "m":
                print("You ordered a Medium beverage for $1.75, would you like to order again?")
            elif size == "l":
                print("You ordered a large beverage for $2.25, would you like to order again?")
    
    while True:
        order= input('Hi, would you like to order anything? Yes (y), No (n)')
        if order == "n":
            print("Ok have a nice day")
            break
        elif order == "y":
            get_order()
        order_again= input('Would you like to order again? Yes (y), No (n)')
        if order_again == "n":
            print("Ok, thanks for your business.")
            break
        elif order_again == "y":
            get_order()
    

    【讨论】:

      【解决方案5】:

      看看这个

      def order_sandwich(key=None):
          if not key:
              type = input(
                  "What Kind of Sandwich Would you like? \n (c) Chiken for $5.25 \n (t) Tofu for $5.75 \n (b) Beef for $6.25 \n (e) No Thanks \n\n Option: "
              )
              menu_sandwich(type)
          elif key == "e":
              print("Thank you")
          else:
              type = input("Please use specifed option [c, t, b]: ")
              menu_sandwich(type)
      
      def order_beverage(key=None):
          order_drink = input(
              "Would you like a beverage? \n (y) Yes \n (n) No \n\n Option: ")
          if order_drink == "y":
              menu_beverage()
          else:
              order_again = input(
                  "Ok Thanks, would you like to order again?\n (y) Yes \n (n) No\n\n Option: "
              )
              if order_again == "y":
                  order_sandwich()
              else:
                  print("ok have a nice day!")
      
      def menu_sandwich(type=None):
          if type == "c":
              print("Chiken\n")
              order_beverage()
          elif type == "t":
              print("Tofu\n")
              order_beverage()
          elif type == "b":
              print("Beef\n")
              order_beverage()
          else:
              order_sandwich(type)
      
      
      def menu_beverage(type=None):
          if type == "s":
              print("Small\n")
              main_order()
          elif type == "m":
              print("Medium\n")
              main_order()
          elif type == "l":
              print("Large\n")
              main_order()
          elif type == "e":
              print("ok have a nice day!")
          else:
              type = input(
                  " (s) small for $1.00 \n (m) medium for $1.75 \n (l) large for $2.25 \n (e) No Thanks \n\n Option: "
              )
              menu_beverage(type)
      
      
      def main_order():
        order = input("Hi, would you like to order anything? \n (y) Yes \n (n) No \n\n Options: ")
        if order == "y":
          order_sandwich()
        else:
          print("ok have a nice day!")
      
      
      main_order()
      
      

      【讨论】:

        猜你喜欢
        • 2014-11-02
        • 1970-01-01
        • 2014-04-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多