私はwhileループを作成しようとしているので、ユーザーはプログラムを継続するかどうかを選択できます。助言がありますか?私はこのプログラムの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枚のカードトリックプログラムであり、これが何であるかについて、いくつかの文脈で持って来ます。あなたが望むならそれを試してみてください。
編集:最後の質問に「y」と入力したときに実際にプログラムを再起動していないということもあります。
はあなたのインデントを修正考えてみましょう。 –
あなたの問題をより具体的にすることはできますか?あなたが助けを求めているこの種の論理制御は非常に基本的なものなので、それは、あなたが何を望んでいるのか分からないと思っています。 –
タイトルにSOLVEDを追加しないでください。受け入れられた答えはあなたのために働いたことを示しています。詳細は[ツアー]をご覧ください。 –