2016-06-13 7 views
0

申し訳ありませんが、私はwhileループについて調べてみましたが、私が見つけたサンプルはあまり役に立ちませんでした。私は人々の事例以外の概念を理解するのに苦労しています。私はPythonには新しく、ほとんどのチュートリアルでは別のシナリオでwhileループを使用しています。だからここに私のコードです:whileループを使用してコードを継続するにはどうすればいいですか?

# This is a guess the number game. 
import random 

# Ask the user what their name is 
print ('Hello. What is your name?') 
name = input() 

# Ask the user if they would like to play a game. 
# If user confirms, game continues 
# If user denies, game ends 
print ('Hi ' + name + ' It is nice to meet you.') 
print ('Would you like to play a game with me?') 

answer = input() 
confirm = ['Yes', 'yes',' Y', 'y', 'Yea', 'yea', 'Yeah', 'yeah', 'Yup', 'yup'] 
deny = ['No', 'no', 'N', 'n', 'Nope', 'nope', 'Nah', 'nah'] 

if answer in confirm: 
    print ('Great! Let\'s get started!') 
elif answer in deny: 
    print ('I am sorry to hear that. Maybe next time? Goodbye') + exit() 

print ('I am thinking of a number between 1 and 20.') 


print ('Can you guess what the number is?') 

secretNumber = random.randint (1, 20) 
print('DEBUG: The secret number is ' + str(secretNumber)) # DEBUG 

for guessesTaken in range (1, 7): 
    print ('Take a guess.') 
    guess = int(input()) 

    if guess < secretNumber: 
    print ('Your guess is to low.') 
    elif guess > secretNumber: 
    print ('Your guess is to high!') 
    else: 
    break # This condition is for the correct guess! 

if guess == secretNumber: 
    print ('Good job, ' + name + '! You guessed the number in ' +  str(guessesTaken) + ' guesses.') 
else: 
    print ('Wrong. The number I was thinking of was ' + str(secretNumber)) 

print ('Would you like to play again?') 
play_again = input() 

if play_again in confirm: 
    print('# Put code to make game restart') 
elif play_again in deny: 
    print ('Thanks for playing!') 

exit()  

私は「確認中play_again場合:」で(ない場合、私は私が必要なものthatsのだと思うので、私を啓発してください)whileループを使用したいと思い、それリターン作るための文"私は1と20の間の数を考えています"という行に戻ります。そうすれば、ユーザーは選択した場合でもゲームを続けることができます。

ありがとうございます。私も最新のPythonを使用しています。

+0

それを投稿する前にコードを紹介してください:)そのような入力短縮することができる冗長なコードの多くもあります – Li357

答えて

0

whileループでコードが追加さ:

# This is a guess the number game. 
import random 

# Ask the user what their name is 
print ('Hello. What is your name?') 
name = input() 

# Ask the user if they would like to play a game. 
# If user confirms, game continues 
# If user denies, game ends 
print ('Hi ' + name + ' It is nice to meet you.') 
print ('Would you like to play a game with me?') 

answer = input() 
confirm = ['Yes', 'yes',' Y', 'y', 'Yea', 'yea', 'Yeah', 'yeah', 'Yup', 'yup'] 
deny = ['No', 'no', 'N', 'n', 'Nope', 'nope', 'Nah', 'nah'] 

if answer in confirm: 
    print ('Great! Let\'s get started!') 
elif answer in deny: 
    print ('I am sorry to hear that. Maybe next time? Goodbye') + exit() 

while True: 

    print ('I am thinking of a number between 1 and 20.') 


    print ('Can you guess what the number is?') 

    secretNumber = random.randint (1, 20) 
    print('DEBUG: The secret number is ' + str(secretNumber)) # DEBUG 

    for guessesTaken in range (1, 7): 
    print ('Take a guess.') 
    guess = int(input()) 

    if guess < secretNumber: 
     print ('Your guess is to low.') 
    elif guess > secretNumber: 
     print ('Your guess is to high!') 
    else: 
     break # This condition is for the correct guess! 

    if guess == secretNumber: 
    print ('Good job, ' + name + '! You guessed the number in ' +  str(guessesTaken) + ' guesses.') 
    else: 
    print ('Wrong. The number I was thinking of was ' + str(secretNumber)) 

    print ('Would you like to play again?') 
    play_again = input() 

    if play_again in confirm: 
    #print('# Put code to make game restart') 
    continue 
    elif play_again in deny: 
    print ('Thanks for playing!') 
    break 

exit() 
0

ここでは、whileループを使用して、何をしようとしているのか簡略化したバージョンです。

import random as rand 
target = rand.randint(1,100) 

found = False 
while not found: 
    print ("***Random Number Guess***") 
    guess = int(input("What is your guess?")) 
    if guess == target: 
     print("Good guess, you found it!") 
     repeat = input("Play again? y/n") 
     if repeat == 'n': 
      found = True 
     elif repeat == 'y': 
      target = rand.randint(1,100) 
      found = False 
    else: 
     if guess < target: 
      print("too low") 
     else: 
      print("too high") 
関連する問題