Click here to Skip to main content
14,826,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have tried to create a guessing game :
Python
from random import randrange
user_name = input("Hello user, please input your name : ")
print("Hello " + user_name + " Welcome to the random number guessing game.\n"
                             "here we will generate a number from 1 to 20\n"
                             "and you will have to guess the number\n"
                             "dont worry, we will help you through ;) ")
out_of_guesses = False
guess_limit=5
guess_count=0
if guess_count == guess_limit:
    out_of_guesses= True

secret_number = int(randrange(20))
answer = int(input("choose a number: "))


while str(secret_number) > str(answer) and not(out_of_guesses):

    print("your number is bigger than the answer")
    guess_count += 1
    print("you have left " + str(guess_count) + " / " + str(guess_limit) + " guesses")
    answer = input("take another guess: ")


while str(secret_number) < str(answer) and not(out_of_guesses):

    print("your number is lower than the answer")
    guess_count += 1
    print("you have left " + str(guess_count) + " / " + str(guess_limit) + " guesses")
    answer = input("take another guess: ")

if secret_number == answer:
    print("You Won, the number is: " + str(secret_number))

if out_of_guesses:
    print("You are out of Guesses")


What I have tried:

for some reason its breaking the loop,
well i have few errors.
please dont laugh, im proggramming for like 3 days.
Posted
Updated 10-Jul-20 7:28am
v3

I would remove the removable...
Try
Python
from random import randrange
user_name = input("Hello user, please input your name : ")
print("Hello " + user_name + " Welcome to the random number guessing game.\n"
                             "here we will generate a number from 1 to 20\n"
                             "and you will have to guess the number\n"
                             "dont worry, we will help you through ;) ")
guess_limit = 5
guess_count = 0

secret_number = int(randrange(20))

while guess_count < guess_limit:

  if guess_count == 0:
    answer = int(input("choose a number: "))
  else:
    answer = int(input("take another guess: "))

  if secret_number < answer:
    print("your number is bigger than the answer")
    guess_count += 1
    print("you have left " + str(guess_count) + " / " + str(guess_limit) + " guesses")
  elif secret_number > answer:
    print("your number is lower than the answer")
    guess_count += 1
    print("you have left " + str(guess_count) + " / " + str(guess_limit) + " guesses")
  else:
    break



if guess_count < guess_limit:
  print("You Won, the number is: " + str(secret_number))
else:
  print("You are out of Guesses")
   
Comments
Patrice T 6-Jul-20 7:29am
   
My 5
CPallini 6-Jul-20 7:33am
   
Thank you.
Maciej Los 7-Jul-20 5:31am
   
5ed!
CPallini 7-Jul-20 13:35pm
   
Thank you very much!
Hi...
I am not that good in Python either, but things that I see...

You have two "while", in my opinion there should be only one and the ifs should be inside the while.

I would go for a while (not solved and not out_of_range)
then the if checks for bigger or smaller to increase the guess_count
then the if checks for solved or limit reached
   
v2
Quote:
im proggramming for like 3 days.

Your code do not behave the way you expect, or you don't understand why !

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.
   
Quote:
im proggramming for like 3 days.
Then stop, take a breath and study a proper tutorial: The Python Tutorial — Python 3.7.8 documentation[^]
   
Try This:

Python
from random import randrange

# use triple quotes (""") to write multiple lines.
prompt_msg = """welcome to the Random Number Guessing Game.
Here we will generate a number from 1 to 20, and you will have 5 guesses!
No worries, we will help you through it! [•‿•]
"""

# prompt user for their name.
user_name = input("Please enter your name: ")

# print greeting to the user including the prompt-msg.
# NOTE: f-strings require python 3.6 or higher.
print(f"\nHello {user_name}, {prompt_msg}")

# set guess_limit to 5.
guess_limit = 5

# set secret_number to a random integer between 0-20.
secret_number = int(randrange(20))

# while you have remaining guesses (i.e. while True)...
while guess_limit > 0:

    # prompt player to guess a number.
    guess = int(input("\nChoose a number: "))

    # If guess is higher than secret_number then,
    if guess > secret_number:

        # decrease guess_count by 1.
        guess_limit -= 1

        # print hint.
        print("Too high! Guess again!")

    # el-If guess is less than secret_number then,
    elif guess < secret_number:

        # decrease guess_count by 1
        guess_limit -= 1

        # print hint
        print("Too low! Guess again!")

    # el-If guess equals secret_number then,
    elif guess == secret_number:

        # print Player wins & reveal secret number
        print(f"You Won, the number is:{secret_number}.")

        # set guess_limit to False - Game ends.
        guess_limit = False

    # print guess_count/guess_limit  ratio
    print(f"\t• You have ({guess_limit}/5) guesses remaining.")

# if there are no guesses remaining then,
if not guess_limit:

    # print message that the game has ended.
    print("\nYou are out of Guesses - Game over!")
   

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