我想使用那些while语句,但显然它不起作用。 我是业余程序员,所以我需要帮助。 我需要它让你在继续之前输入一个有效的答案,但我不知道该怎么做。如果你看一下,你应该能够看到我想做的事。
import random
print('Hello, What is your name?')
name = input()
print('Do you want to play a game?') # Asks if you want to play a game.
myAnswer = input() # Stores your answer.
while myAnswer != ('yes'):
print('Please enter a valid answer.')
myAnswer = input()
while myAnswer != ('Yes'):
print('Please enter a valid answer.')
myAnswer = input()
while myAnswer != ('no'):
print('Please enter a valid answer.')
myAnswer = input()
if myAnswer == ('yes'):
print('Well, ' + name + ', I am thinking of a number between 1 and 15.')
elif myAnswer == ('Yes'):
print('Well, ' + name + ', I am thinking of a number between 1 and 15.')
else:
print('Maybe next time.')
raise SystemExit
secretNumber = random.randint(1, 15)
for guessesTaken in range(1, 6):
print('Take a guess.')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low.')
elif guess > secretNumber:
print('Your guess is to high.')
else:
break # This is for the correct answer.
if guess == secretNumber:
print('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope, The number I was thinking of was ' + str(secretNumber))
答案 0 :(得分:0)
这很简单:
import random
print('Hello, What is your name?')
name = input()
print('Do you want to play a game?') # Asks if you want to play a game.
myAnswer = input() # Stores your answer.
while myAnswer != ('yes') and myAnswer != ('Yes') and \
myAnswer != ('no') and myAnswer != ('No'):
print('Please enter a valid answer.')
myAnswer = input()
if myAnswer == ('yes') or myAnswer == ('Yes'):
print('Well, ' + name + ', I am thinking of a number between 1 and 15.')
else:
print('Maybe next time.')
raise SystemExit
secretNumber = random.randint(1, 15)
for guessesTaken in range(1, 6):
print('Take a guess.')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low.')
elif guess > secretNumber:
print('Your guess is to high.')
else:
break # This is for the correct answer.
if guess == secretNumber:
print('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope, The number I was thinking of was ' + str(secretNumber))
你刚刚忘记了Python中的缩进=)
答案 1 :(得分:0)
非常简单:
import random
a=random.randint(0,100)
print("Guess a number from 0 - 100")
x=int(input("guess a number "))
while(x<100):
if(a==x):
print("You guessed it correctly")
break
elif(x<a):
print("guess higher")
x=int(input("guess another number "))
elif(x>a):
print("guess lower")
x=int(input("guess another number "))
进行你想要的简单改动并将其作为游戏玩:)祝愿!