抱歉,我尝试过研究while循环,我发现的例子对我没什么帮助。我很难理解人们的例子之外的概念。我是python的新手,大多数教程在不同的场景中使用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()
我想使用while循环(因为我认为这就是我需要的,如果没有,请赐教我)在“if play_again in confirm:”语句中让它返回到“我正在考虑一个数字在1到20“之间。这样,用户可以选择继续玩游戏。
提前谢谢你。我也在使用最新的Python。
答案 0 :(得分: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")
答案 1 :(得分: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()