Python - I'm stuck.

Will

Senior Administrator
Staff member
Joined
Mar 4, 2012
Posts
8,197
Location
%tmp%
So I was doing some exercises to help me learn python - I had to give it a break during exams, but I'm learning again. I can't get this bit of code to work, there is a logical error in it somewhere which I can't spot.

The idea is, the user types a number and then the computer tries to guess it. It gets 20 attempts, if it goes through all 20, then the user wins and the game ends - if it guesses the correct number, it's SUPPOSED to break the while loop and tell the user that the computer won. I can't get it to work, all that happens is it goes through all 20 attempts (I've tried 100 attempts for testing to make sure it hits the number without having to run it 10 times) - after it's reached attempt 20 it tells the user that they won - even if it had guessed the number.

Code:
# The number guessing game
#
# The computer tries to guess your number within 20 tries.

# Random import

import random

# Introduction

print("\tThe Number Guessing Game")
print("\t--Created by me")

print("\n\nWelcome to the game. To play, pick a number between 1 and 100")

number = input("What is your number? ")

tries = 1
count = 1

while tries <=20 and count == 1:
    guess = random.randint(1, 100)
    print("\n", guess)

    if guess == number:
        print("\n The computer has guessed your number!")
        count += 1

    tries += 1

if tries <=20:
    print("\nThe computer has won! It guessed, ", number)
    print("Thanks for playing!")

elif tries >20:
    print("\nCongratulations! You have won! The computer couldn't guess your number.")

input("\n Press Enter to exit the program")

This always results in the while loop completing, even if the computer hits the right number. I've tried using a break, as below, but it still doesn't work in the above code. What is confusing me most, is that the code below works fine:

Code:
import random

tries = 1
number = 42

while tries <=20:
    guess = random.randint(1, 100)
    print("\n", guess)

    if guess == number:
        print("\n The computer has guessed your number!")
        break
    tries += 1

input("")

If the computer hits 42, the while loop breaks. Have I messed up the user input somehow? I've tried both the count, and break method to break the loop - now I'm stuck :D.
 
I fixed it :D. Problem line:

Code:
number = input("What is your number? ")

should be....

Code:
number = int(input("What is your number? "))

I was assigning the user input to be a string, not an integer as intended.... oooops. :banghead:

------------

Hopefully my next thread here will be to show off some amazing program that's a lot better than this one... or at least a less simple problem. :grin1:
 
Last edited:
Just for the hell of it, I decided to refactor. Works exactly the same, with one of the conditional statements cut out:

Code:
# The number guessing game
#
# The computer tries to guess your number within 20 tries.

# Random import

import random

# Introduction

print("\tThe Number Guessing Game")
print("\t--Created by me")

print("\n\nWelcome to the game. To play, pick a number between 1 and 100")

number = int(input("What is your number? "))

tries = 1
guessed = False

while guessed == False and tries <=20:
    guess = random.randint(1, 100)
    
    if guess == number:
        print("\n The computer has guessed your number! It is", guess)      
        guessed = True        
        
    tries += 1

if tries >20:
    print("\nCongratulations! You have won! The computer couldn't guess your number.")

input("\nThanks for playing! Press Enter to exit the program")
 
That is neater, thanks :thumbsup2:. I now have a new problem (see my new thread), but another question here.

At this point: number = int(input("What is your number? "))

How can I make this more foolproof, so that if the user mistypes or doesn't enter a number it won't generate errors? I want this to happen:

"What is your number?"
cheese
"Please enter a number. What is your number?"
cheese
"Please enter a number. What is your number?"
... etc
 
At this point: number = int(input("What is your number? "))

How can I make this more foolproof, so that if the user mistypes or doesn't enter a number it won't generate errors? I want this to happen:

"What is your number?"
cheese
"Please enter a number. What is your number?"
cheese
"Please enter a number. What is your number?"
... etc

Looks like you had the right idea, in your code in the other thread:
Code:
    while response not in range(low, high):
        response = int(input(question))
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top