Click here to Skip to main content
15,667,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm just stuck in solving this number guess game program. I was given the program to correct (shown below). The successful output should look like this:

I am thinking of a number between 1 and 100.
You have 10 guesses left. Take a guess.
> 50
Your guess is too high.
You have 9 guesses left. Take a guess.
> 25
Your guess is too low.
You have 8 guesses left. Take a guess.
> 37
Your guess is too low.
You have 7 guesses left. Take a guess.
> 42
Your guess is too high.
You have 6 guesses left. Take a guess.
> 40
Your guess is too low.
You have 5 guesses left. Take a guess.
> 41
Yay! You guessed my number!

What I have tried:

import random

def askForGuess():
  while True:
    guess = input('> ') 

    if guess.isdecimal():
      return int(guess)
    print('Please enter a number between 1 and 100.')

secretNumber = random.randint(1, 100)
print('I am thinking of a number between 1 and 100.')

for i in range(0,10,2):
    print('You have {10 - i} guesses left. Take a guess.')

    guess == askForGuess()
    if guess == secretNumber:
        break

    if guess < secretNumber:
        print('Your guess is too high.')
    if guess > secretNumber:
        print('Your guess is too low.')

if guess == secretNumber:
    print('You guessed my number!')
else:
    print('No more guesses. The number I was thinking of was', secretNumber)
Posted
Updated 8-Jun-22 12:18pm

This assignment is an exercise in debugging. You have to follow the code line-by-line and "run" it in your head, keeping track of values and executing each statement looking for the statement that don't make sense or don't do what's expected.

Anyone doing the work for you is completely missing the point of the assignment and hot helping you. This is a skill you MUST learn on your own, by doing it.
 
Share this answer
 
Quote:
Hi, I'm just stuck in solving this number guess game program. I was given the program to correct

Being able to correct other's code is a key skill for a programmer.
You will get this skill by practicing, and the more you practice, the better you get.
So you are told there is a bug:
First thing, run the program and see where the language chocks. It tells you where the source code is not Python anymore.
Then read the code at position of error and see if you find something wrong. If you don't find, read again very carefully.
If you don't find again, retype the line of code in another place and then insert it near the offending line.
compare original line with you own line, is there something different ? (in this case, it should be something different)

Sometimes, the problem is in the logic, in this case, the debugger is the tool of choice.
There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900