• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Python Guessing Game Program with Error - Assistance Needed

noahshine15

New Coder
I'm excited to share with you a small Python program I've been working on, although it does currently have some errors. I decided to use an online Python editor to thoroughly examine and debug the code. You can access this convenient online editor by clicking on the following link: Python Online Editor.

website link:
Python Online Editor and Compiler

Here's the code:

Python:
import random

secret_number = random.randint(1, 100)
attempts = 0

print("Welcome to the Guessing Game!")
print("I have selected a random number between 1 and 100.")
print("Try to guess the number.")

while True:
    guess = random.randint(1, 100)
    attempts + = 1 

    if guess < secret_number:
        print(f"Attempt {attempts}: Guessed {guess}. It's too low. Try a higher number.")
    elif guess > secret_number:
        print(f"Attempt {attempts}: Guessed {guess}. It's too high. Try a lower number.")
    else:
        print(f"Congratulations! You've guessed the secret number {secret_number} in {attempts} attempts.")
        break

print("Thank you for playing!")
 
I'm excited to share with you a small Python program I've been working on, although it does currently have some errors. I decided to use an online Python editor to thoroughly examine and debug the code. You can access this convenient online editor by clicking on the following link: Python Online Editor.

website link:
Python Online Editor and Compiler

Here's the code:

Python:
import random

secret_number = random.randint(1, 100)
attempts = 0

print("Welcome to the Guessing Game!")
print("I have selected a random number between 1 and 100.")
print("Try to guess the number.")

while True:
    guess = random.randint(1, 100)
    attempts + = 1

    if guess < secret_number:
        print(f"Attempt {attempts}: Guessed {guess}. It's too low. Try a higher number.")
    elif guess > secret_number:
        print(f"Attempt {attempts}: Guessed {guess}. It's too high. Try a lower number.")
    else:
        print(f"Congratulations! You've guessed the secret number {secret_number} in {attempts} attempts.")
        break

print("Thank you for playing!")
Hi there,
So, you're gonna hate yourself over this lol, and trust me, everyone hates themselves when they realize their stress was all over the smallest issue :D

So, you have an unwanted space between "+" and the "=" signs in the second line after the while declaration
replace it with this:
Python:
attempts += 1
 
Hi there,
So, you're gonna hate yourself over this lol, and trust me, everyone hates themselves when they realize their stress was all over the smallest issue :D

So, you have an unwanted space between "+" and the "=" signs in the second line after the while declaration
replace it with this:
Python:
attempts += 1
Hi Antero360,

Haha, don't worry! It happens to the best of us. Thank you so much for catching that detail. I really appreciate your keen eye for these things. It's amazing how even the tiniest things can make a big difference in code!

I've made the correction, and everything is working perfectly now. Thanks again for your help!:blush:🙌
 
Hi Antero360,

Haha, don't worry! It happens to the best of us. Thank you so much for catching that detail. I really appreciate your keen eye for these things. It's amazing how even the tiniest things can make a big difference in code!

I've made the correction, and everything is working perfectly now. Thanks again for your help!:blush:🙌
No problem 😁 Glad to hear everything is working as intended.
 
Here is a link to the share section of the forum of some code I wrote a while back.
It's a number guessing game using tkinter.
 
  1. Replaced random.randint(1, 100) with int(input("Enter your guess: ")) to take input from the user instead of generating a random guess.
  2. Changed attempts + = 1 to attempts += 1 to fix the syntax error.
Now, the player will be prompted to enter their guess, and the game will respond accordingly.
 
Here are the changes made:



Code:
Python:
import random

secret_number = random.randint(1, 100)
attempts = 0

print("Welcome to the Guessing Game!")
print("I have selected a random number between 1 and 100.")
print("Try to guess the number.")

while True:
    guess = int(input("Enter your guess: "))
    attempts += 1

    if guess < secret_number:
        print(f"Attempt {attempts}: Guessed {guess}. It's too low. Try a higher number.")
    elif guess > secret_number:
        print(f"Attempt {attempts}: Guessed {guess}. It's too high. Try a lower number.")
    else:
        print(f"Congratulations! You've guessed the secret number {secret_number} in {attempts} attempts.")
        break

print("Thank you for playing!")
Anyone Can Learn to Code! 550+ Hours of course content!
 
Last edited:
Here is a way to add a little error checking.

Python:
from random import randint

random_number = randint(1,101)
tries = 0

while guess:= input('Enter a number between 1 - 100\n >> '):
    if guess.isdigit():
        tries += 1
        guess = int(guess)
        if guess > random_number:
            print('Number is too high.')
        elif guess < random_number:
            print('Number is too low...')
        else:
            print(f'Congradulations! You guessed the number in {tries} tries.')
            break
    else:
        print('Error! Only whole numbers are allowed')

Using a try block
Python:
guessing = True
while guessing:
    try:
        guess = int(input('Enter a number between 1, 100\n>> '))
        tries += 1
        if guess > random_number:
            msg = f'{guess} is too high. Please try again.'
        elif guess < random_number:
            msg = f'{guess} is too low. Please try again'
        else:
            msg = f'Congradulations! You guessed the number in {tries} tries.'
            guessing = False
        print(msg)

    except ValueError:
        print('Error! Only whole numbers are allowed.')
 
Last edited:
Top Bottom