Python Forum
Guess the number game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guess the number game
#1
I have a guess the number game assignment this is my code so far was hoping someone could fix it for me


import random
 
hidden = random.randrange(1, 100)

 
guess = int(raw_input("Please enter your guess: "))
 
if guess == hidden:
    print ("Hit!")
elif guess < hidden:
    print ("Your guess is too low")
else:
    print ("Your guess is too high")
Reply
#2
We aren't here to do the work for you. What needs fixing and what have you tried to fix it?
Reply
#3
Use input, not raw_input.
Now, do you want to allow more than one guess? How would you structure that loop?
Reply
#4
You are taking the guess as an integer value but in the "if statement" you are checking whether its equal to a string value. This is wrong since you are trying to compare an integer value with a string value.

Also raw_input() function is for Python 2, use input() function for Python 3.
Reply
#5
@jefsummers and @Pranav, point that raw_input is python2, and input is python3 equivalent. If you want to get the difference better - read https://python-forum.io/Thread-Python3-2...-raw-input
But their remarks don't make it very clear - you are using python2 (otherwise raw_input will raise error) and you should be using python3, not python2. Support for python2 ended January 1st, 2020. Of course that is if it's not school requirement to use python2 (bad sign for the course IMHO)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Mar-03-2020, 07:13 AM)Pranav Wrote: You are taking the guess as an integer value but in the "if statement" you are checking whether its equal to a string value. This is wrong since you are trying to compare an integer value with a string value.

Also raw_input() function is for Python 2, use input() function for Python 3.

hidden evaluates to an integer (random.randrange(1,100). So, that should not be a problem.

What is needed is a loop structure which keeps asking for a new guess (based on the evaluation of if.. elif ... else conditions) until the hidden number is guessed.

Hope that helps
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help - random number/letter guessing game juin22 1 3,152 Aug-16-2020, 06:36 AM
Last Post: DPaul
  Dynamically chosing number of players in a "game" nsadams87xx 5 4,043 Jul-17-2020, 02:00 PM
Last Post: deanhystad
  Can someone help me optimize this game for large number of strings Emekadavid 13 4,778 Jul-06-2020, 06:16 PM
Last Post: deanhystad
  making a guessing number game blacklight 1 2,154 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Asking for help in my code for a "Guess the number" game. Domz 5 3,747 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Guess a number game Drone4four 4 5,181 Nov-16-2018, 03:56 AM
Last Post: Drone4four
  guess the number atux_null 8 6,009 Dec-06-2017, 09:14 AM
Last Post: gruntfutuk
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,735 Oct-06-2016, 12:49 AM
Last Post: tinabina22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020