我在这个程序的while循环中需要帮助

时间:2017-11-28 19:14:28

标签: python

所以我正在尝试创建一个while循环,以便用户可以选择是否要继续该程序。有什么建议吗?

import random

while True: 
print ("--------------------------------------------------------------------\n")
name = input("Please enter your name: ")
pack = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
random.shuffle(pack)
print ("Welcome {0}! Hope you have fun playing!  \n".format(name))
print("Original deck:", pack, "\n")
print ("--------------------------------------------------------------------\n")

for i in range(3):
    pack1 = pack[::3]
    pack2 = pack[1::3]
    pack3 = pack[2::3]

    print("1: ", pack1, "\n")
    print("2: ", pack2, "\n")
    print("3: ", pack3, "\n")

    user = input("Pick a number and enter the row it is in: ")
    while not (user == "1" or  user == "2" or  user == "3"):
        print(user, " is not a valid answer. Please try again \n")
        user = input("Pick a number and enter the row it is in: ")


    if user == "1":
        pack = pack3 + pack1 + pack2

    elif user == "2":
        pack = pack1 + pack2 + pack3

    elif user == "3":
        pack = pack2 + pack3 + pack1

print("The number you are thinking of is:", pack[10], "\n")

answer = input("Would you like to play again (y/n)? ")
if answer != "y" or answer != "n":
        break
print ("Please press 'y' or 'n' and then Enter... ")
if answer == "y":
        continue
else:
        print ("Thank you for playing!")
        break

只是为了介绍一下这是什么背景,这是一个21 Card Trick程序。如果你愿意,可以尝试一下。

编辑:当问到最后的问题时,当你输入'y'时它并没有真正重启程序时发生了什么。

2 个答案:

答案 0 :(得分:0)

使用控制布尔值来处理用户参与的状态。

Vasilis G.指出,你的while循环缩进也是错误的。



import random

controlFlag = True #add boolean control

while controlFlag == True: 
  print ("--------------------------------------------------------------------\n")
  name = input("Please enter your name: ")
  pack = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
  random.shuffle(pack)
  print ("Welcome {0}! Hope you have fun playing!  \n".format(name))
  print("Original deck:", pack, "\n")
  print ("--------------------------------------------------------------------\n")

  for i in range(3):
      pack1 = pack[::3]
      pack2 = pack[1::3]
      pack3 = pack[2::3]

      print("1: ", pack1, "\n")
      print("2: ", pack2, "\n")
      print("3: ", pack3, "\n")

      user = input("Pick a number and enter the row it is in: ")
      while not (user == "1" or  user == "2" or  user == "3"):
          print(user, " is not a valid answer. Please try again \n")
          user = input("Pick a number and enter the row it is in: ")


      if user == "1":
          pack = pack3 + pack1 + pack2

      elif user == "2":
          pack = pack1 + pack2 + pack3

      elif user == "3":
          pack = pack2 + pack3 + pack1

  print("The number you are thinking of is:", pack[10], "\n")

  answer = input("Would you like to play again (y/n)? ")
  if answer == "y":
      controlFlag = True # unnecessary, left in for completeness.

  elif answer == 'n':
      print ("Thank you for playing!")
      controlFlag = False
      
  else:
      print('wrong choice')
      break




答案 1 :(得分:0)

一般主循环结构通常有点像这样:

def func():
  while True
    #run your game or whatever
    #ask for input somehow
    if input==truthy:
      break 
func()