vendredi 28 juin 2019

Python - Is it possible to use a list as a condition for an if statement?

I am in the process of learning Python and one of the exercises that I was tried to do was make a "guess the number" game. I made a very simple one, but now I want to take it a little bit further and set some boundaries for the inputs so that the program is error-proof. Here is my code:

# Guess the number game.

import random

print('Hello. What is your name?')

yourName = input() # collects user's name

solution = random.randint(1,20)

print('Well, ' + str(yourName) + ', I am thinking of a number between 1 and 20.')

acceptable = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20'] # acceptable answers

def game():

    for attempts in range(1,6):

                    print('Take a guess. You have ' + str(6 - attempts) + ' attempt(s) remaining')

                    # try:

                    guess = (input())

                    while guess:

                                if guess != acceptable:

                                    print("That is not a valid answer!")

                                    guess = (input())

                                else:

                                    moveon()

def moveon():

                    while guess != solution:

                                if guess < solution:

                                    print('Your guess is too low. Try again.')

                                elif guess > solution:

                                    print('Your guess is too high. Try again.')

                                else:

                                    endofgame()

'''
                    except ValueError:

                            print('Please enter a guess that is a number.')

                            attempts + 1 == attempts
'''

def endofgame():

    if guess == solution:

            print('You guessed it! It took you ' + str(attempts) + ' attempt(s)')

            playAgain()

    else:

            print('Game over! The number I was thinking of was ' + str(solution))

            playAgain()



# Option to play again with a new number

def playAgain():

    global solution

    print('Play again? Y/N')

    playAgain = input()

    if playAgain == 'Y':

            print('Okay, ' + str(yourName) + ', I have another number between 1 and 20.')

            solution = random.randint(1,20)

            game()

    else: print('Thanks for playing!')



# Start game
game()

So what I want to do is make sure that when the user is prompted to input a number between 1 and 20, entering answers like "22", "coffee" or "14.5" would be invalid and prompt them to try again and enter a valid answer. However, when I run this program right now, any answer that is entered is returned as invalid. How do I make it so that only certain answers are accepted, and others are not? I suspect that there is a way other than using a list that I do not know of yet. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire