簡體   English   中英

Python循環錯誤中的隨機數猜測游戲

[英]Random Number Guessing Game In Python Loop Error

我想使用那些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))

這很簡單:

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中的縮進=)

這很簡單:

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 "))

進行您想要的簡單更改,然后將其作為游戲玩:)祝福!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM