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
#2
Welcome to the Python forum,

A small script like yours does not have to be a function.

See the following link for use of a while loop. http://www.pythonforbeginners.com/contro...hile-loops
Seed the value outside of the loop, and then loop until an answer is obtained. Use an input() statement to get the guess from the user.

Additional items to consider after you are successful:
a. What if the user types in something other than a number?
b. What if the user wants to give up?


Please let us know if you need additional help.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#3
This is a fixed edit of your code
import random
it=random.randint(0, 101)
def main():
    x=int(input('Guess a number one through one hundred'))
    if x == it:
        print("You got it!")
    elif x > it:
        print("too high")
        main()
    else:
        print("too low")
        main()
main()
Reply
#4
Recursive invocation is limited by depth.

count = 0
def main():
    global count
    count += 1
    return count

main()
This code leads to

Output:
File "<stdin>", line 4, in main File "<stdin>", line 4, in main RuntimeError: maximum recursion depth exceeded >>> count 999
So, the program can't produce too much queries (but this behavior (recursion depth) could be tweaked).

Using infinite loop is more preferable in this case.

from random import randint
it = randint(0, 101)
def guess():
    while True:
        x = int(input('Guess a number one through one hundred: '))
        if x == it:
            print("You got it!")
            break
        elif x > it:
            print("too high")
        else:
            print("too low")
guess()
Reply
#5
here a more sophisicated one:
import random

print("gues_number.py")
print("GUESS NUMBER >1 AND <100 ")
answer = random.randint(1, 99)
print(answer)

maximum = 100
maximum2 = 100
minimum = 1
new_min = 1
new_max2 = 100

print(minimum, "<your number<", maximum)

while True:
    guess = int(input())
    if guess == answer:
        print("bingo answer is", answer)
        break
    elif guess < answer:
        print("wrong answer, guess larger")
        new_max = guess
        new_min = guess
        print(guess, "<?<", new_max2)
    else:
        answer < guess
        print("wrong answer, guess smaller")
        new_max2 = guess
        new_min2 = guess
        print(new_min, "<?<", guess)
input("Press ENTER to continue for another guess file")
secret_number = 34
tries, guess = 0, 0
while guess != secret_number:
    guess = int(input("Guess a number (<100): "))
    if guess > secret_number:
        print("Lower...")
    elif guess < secret_number:
        print("Higher...")
    tries += 1

print('You guessed it! The number was {} in {} tries'.format(guess, tries))
#input("Press ENTER to continue")
Reply
#6
Hmm that's a two code put together @sylas,i do recognize a demo i did in a post before secret_number = 34 Wink
But the first part should not be there,it's a different code.
Here how my code should look,as you just copy my hint in the other post.
So the they point here is that there no break,code run as long as not the secret number is guessed.
import random

secret_number = random.randint(1, 100)
tries, guess = 0, 0
while guess != secret_number:
    guess = int(input("Guess a number (<100): "))
    if guess > secret_number:
        print("Lower...")
    elif guess < secret_number:
        print("Higher...")
    tries += 1

print('You guessed it! The number was {} in {} tries'.format(guess, tries))
Output:
λ python guess.py Guess a number (<100): 50 Lower... Guess a number (<100): 25 Higher... Guess a number (<100): 35 Lower... Guess a number (<100): 30 Higher... Guess a number (<100): 32 Higher... Guess a number (<100): 34 Lower... Guess a number (<100): 33 You guessed it! The number was 33 in 7 tries
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 3,027 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  Ask again if wrong guess with While. EduPy 4 2,258 Oct-21-2021, 07:46 PM
Last Post: menator01
  I guess it's about print tsavoSG 2 2,116 Feb-08-2021, 08:34 PM
Last Post: steve_shambles
  can't figure out problem with number guess Galoxys 4 3,350 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,214 Jan-07-2018, 04:26 AM
Last Post: squenson
  "Guess the Number" game CTT 14 13,346 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