简体   繁体   中英

Random Number Guessing Game In Python Loop Error

I want to use those while statements but obviously it doesn't work. I am a amateur programmer, So I need Help. I need it to make you enter a valid answer before continuing, but I don't know how to do it. If you look at it you should be able to see what I want to do.

This is a random number guessing game.

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

It is very simple:

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

You have just forgot about indents in Python =)

It is Very Easy:

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

Make simple alterations you want and play it as a game :) Wishes!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM