Marlee3jackson
Marlee3jackson

Reputation: 47

Guessing game program with a while loop, where users enter a number and the computer tells them if it right, or to guess higher, or lower

My program continues to tell the user to guess lower, even if the user types in the correct number. How do I solve this?

import random 
a=raw_input('enter a number')
b= random.randrange(0,11)
while a!=b:
    if a < b:
        print ('you are not correct, try a higher number')
    if a > b:
        print('you are not correct,  try a lower number')
    if a== b:
        print('wwcd')
    print b

Upvotes: 0

Views: 480

Answers (1)

Knells
Knells

Reputation: 837

The problems with it as currently are that a is never updated & that you are using 'a' the character rather than a the variable

while `a`!=b: 

Compares a constant character to the number b (and will always be larger). It should be:

while a!=b:   

This change should be applied to all your conditional statements (also it's probably best to remove the repeated if 'a'== b blocks, as only one is needed)

For the next part you need to update a as part of the loop (such that the user can change the input). You only need to move the part where you assign a a value downwards:

while a!=b:    
    a=raw_input('enter a number')
    //rest of your conditionals statements

EDIT:

You have a 3rd problem. The raw_input() function returns a string and you need an int for comparison. To fix it simply cast it to int: int(raw_input('Enter a number')) or, more appropriately use Python 2.x's input() function. This will evaluate anything you input so will return int when you enter a number. But watch out, Python 3.x input() acts like raw_input() in 2.x and raw_input() is gone.

Upvotes: 1

Related Questions