2

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!

asked Jun 28, 2019 at 22:59
3
  • guess != acceptable is checking the entered value against the entire list itself - which can never be equal, since the value being checked is a string instead of a list. Commented Jun 28, 2019 at 23:03
  • Check if the type of the input to ensure it is a int.
    gregory
    Commented Jun 28, 2019 at 23:03
  • Also, you could use something like ``` if guess not in range(1,21) ``` Commented Jun 29, 2019 at 5:22

2 Answers 2

3

You need to check if an item is not contained in a list, this is how we do it in Python:

if guess not in acceptable:
answered Jun 28, 2019 at 23:01
1

Instead of using if guess != acceptable you want to use if guess not in acceptable.

With python, you can check if an element exists inside of an array by using the in command.

answered Jun 28, 2019 at 23:05
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.