Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number Guessing Game in Python

Tags:

python

random

I was trying to create a simple random number guessing game. The problem is even if I type the correct number it replies with a 'The number is less than'. Can somebody provide me a solution for this one ?

Thanks 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
like image 495
user2420593 Avatar asked Nov 29 '22 13:11

user2420593


2 Answers

The biggest problem is that you're not saving the conversion to int so you're using the guess as the string the user entered. You need to save it by doing user = int(raw_input('>>>'))

There are other ways you can improve this code, however. You repeat yourself a bit, and you don't need random.choice, you can use random.randrange(1, 10)

You shouldn't just say except:. You wanna only catch the exceptions you are looking for. The particular exception you are looking for is a ValueError

Additionally, I suggest you let the user try again when they enter something that's not a number. You can wrap up the whole thing in it's 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
like image 94
Ryan Haining Avatar answered Dec 16 '22 11:12

Ryan Haining


When you convert to int(user), you aren't saving a new int to user. So user still remains a string.

What you need to do is

user = int(user)

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

like image 43
James Avatar answered Dec 16 '22 10:12

James