Guessing Game Number in Python

I tried to create a simple game for guessing random numbers. The problem is even that I am dialing the correct number that he answers with "Number is less." Can someone provide me a solution for this?

Thank you in advance

import random import sys numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] user = raw_input('Guess The Number\n Pick between 1 - 10\n >>> ') try: int(user) except: print "Numbers Only !" sys.exit(0) number = random.choice(numbers) int(number) for i in range(0, 4): if number == user: print 'You Won!' if user > number: print 'The number is less than', user user = raw_input('>>> ') try: int(user) except: print "Numbers Only !" if user < number: print 'The number is bigger than', user user = raw_input('>>> ') int(user) print "The Number was", number 
+4
source share
3 answers

The biggest problem is that you are not saving the conversion to int , so you use the assumption as a string entered by the user. You need to save it by doing user = int(raw_input('>>>'))

There are other ways to improve this code. You repeat yourself a bit and you do not need random.choice , you can use random.randrange(1, 10)

You cannot just say except: You only want to catch the exceptions you are looking for. The special exception you are looking for is ValueError

In addition, I suggest that you try again when they enter something, not a number. You can complete all this in your own function.

 import random def get_user_num(msg='>>> '): """Print the msg parameter as a prompt for the user to enter a number. If they enter an invalid string, reprompt them until they enter a number. """ while True: try: return int(raw_input(msg)) # save the conversion to int except ValueError: # only except the error you're actually looking for print 'Numbers Only!' # 'from 1-9' is probably better than 'between 1-10' user = get_user_num('Guess The Number\n Pick from 1-9\n>>> ') number = random.randrange(1, 10) # <- numbers list is unnecessary #int(number) # this conversion was never needed, it was already a number for _ in range(4): # you don't need (0, 4), 0 is assumed if number == user: print 'You Won!' # the correct number has been guessed break # exit the loop once the number has been correctly guessed elif user > number: print 'The number is less than', user elif user < number: print 'The number is bigger than', user # Don't repeat yourself, put this outside the `if`s user = get_user_num() else: #only print the answer when it wasn't guessed correctly print "The Number was", number 
+12
source

When you convert to int (user), you are not saving a new int for the user. Thus, the user is still a string.

What you need to do is

 user = int(user) 

By the way, this is for all places where you use int (user)

+6
source

This can be done with a much simpler implementation:

 import random number = random.randrange(10) for i in xrange(4): try: user = int(raw_input('guess: ')) except ValueError: print 'must be int' continue if user == number: print 'bravo' break elif user < number: print 'greater' else: print 'lesser' print 'it was: %d' % number 
+3
source

Source: https://habr.com/ru/post/1482732/


All Articles