Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guess a number
#1
Hello I a beginner trying to practice after completing the codeacademy course. Right now im trying to do a guess a number game.

This is the instructions

The Goal: Similar to the first project, this project also uses the random module in Python. The program will first randomly generate a number unknown to the user. The user needs to guess what that number is. (In other words, the user needs to be able to input information.) If the user’s guess is wrong, the program should return some sort of indication as to how wrong (e.g. The number is too high or too low). If the user guesses correctly, a positive indication should appear. You’ll need functions to check if the user input is an actual number, to see the difference between the inputted number and the randomly generated numbers, and to then compare the numbers.

This is my code

from random import randint
def guess(x):
  it = randint(0, 101)
  if x == it:
      print("You got it!")
  elif x > it:
      print("too high")
  else:
      print("too low")
print guess(50)
The problem with it is I only get one chance to guess. I think i need to use a while statement and some sort of +- symbol, but it kept turning out wrong.
Reply


Messages In This Thread
Guess a number - by Ameen - Apr-02-2018, 01:03 AM
RE: Guess a number - by ljmetzger - Apr-02-2018, 09:22 PM
RE: Guess a number - by Ares - Apr-03-2018, 01:50 AM
RE: Guess a number - by scidam - Apr-03-2018, 03:35 AM
RE: Guess a number - by sylas - Apr-03-2018, 07:28 AM
RE: Guess a number - by snippsat - Apr-03-2018, 01:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 3,078 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  Ask again if wrong guess with While. EduPy 4 2,299 Oct-21-2021, 07:46 PM
Last Post: menator01
  I guess it's about print tsavoSG 2 2,153 Feb-08-2021, 08:34 PM
Last Post: steve_shambles
  can't figure out problem with number guess Galoxys 4 3,405 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,272 Jan-07-2018, 04:26 AM
Last Post: squenson
  "Guess the Number" game CTT 14 13,506 Jul-26-2017, 11:41 AM
Last Post: sylas

Forum Jump:

User Panel Messages

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