2017-11-28 23 views
0

私は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」と入力したときに実際にプログラムを再起動していないということもあります。

+4

はあなたのインデントを修正考えてみましょう。 –

+0

あなたの問題をより具体的にすることはできますか?あなたが助けを求めているこの種の論理制御は非常に基本的なものなので、それは、あなたが何を望んでいるのか分からないと思っています。 –

+0

タイトルにSOLVEDを追加しないでください。受け入れられた答えはあなたのために働いたことを示しています。詳細は[ツアー]をご覧ください。 –

答えて

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

+0

申し訳ありませんが、私は明確ではありません。私はあなたが私を助けてくれてありがとう! :) – Snoodie

0

一般的なメインループ構造は、通常、多少このように書きます:

def func(): 
    while True 
    #run your game or whatever 
    #ask for input somehow 
    if input==truthy: 
     break 
func() 
関連する問題