# Guessing a random number [closed]

The program asks for a user to set the max number of the range. That number is used to set the max range of a randomly generated number. The program keeps track of the number of guesses. I have taken a couple classes recently on different programming languages. I wrote a Hi/Low program in C a while back and decided to convert it to Python 3. I would appreciate someone looking at the code of the program and give me feedback on what I did good/bad and ways that I could improve.

def tooHigh():
return guess

def tooLow():
return guess

def guessLoop(x):
guess=x
counter+=1
if guess>gennum:
guess = tooHigh()
elif guess<gennum:
guess = tooLow()
elif guess==gennum:
print("you guessed it!")
return guess

import random
range=int(input("Please enter the maximum number of the range you would like to guess within: "))
gennum = random.randrange(range+1)
guess = int(input("Please enter a number within the range: "))
counter =1

if guess==gennum:
print("Congrats, you guessed right on the first try!")

while not guess==gennum:
loop(guess)

print("It took you",counter,"guesses to guess correctly.")

• Why do you print the solution before the player make any attempt? Is it because you don't want them to fail so your program doesn't run into 2 NameErrors? – 301_Moved_Permanently Oct 23 '16 at 21:30
• that is because I forgot to delete it from the code after testing it. I wanted to make sure I knew what the number was so I could intentionally guess wrong to make sure the count was correct and that the correct functions were being called. What name errors are you referencing? – platypus87 Oct 23 '16 at 21:31
• And so? Did you test it while guessing wrong? Turns out this doesn't work unless you guess right first try. – 301_Moved_Permanently Oct 23 '16 at 21:38
• I originally said it was working for me, because it was when I used my IDE. But not so much when I ran it from command line...my apologies – platypus87 Oct 23 '16 at 21:42
• Then if you have a working version, please, post it. This one has at least 4 issues: NameError, UnboundLocalError, TypeError and infinite loop. Culprits being loop, counter, print and... return, I guess… – 301_Moved_Permanently Oct 23 '16 at 21:46

1) There are three different places in the code where you ask the user to enter the next number to guess. This is not very readable and will backfire if and when you want to extend this code. It's better to have something like this:

def getNextGuessFromUser(messageToPrint):
print messageToPrint + '\n'
return int(input())


Then remove the tooHigh, tooLow and initial guess calls, and replace them with calls to this function. By the way, I'm not sure how your reading of guesses works with the print call; looks like a defect to me.

2) Unnecessary usage of global variables. guess should be generated for the first time inside guessLoop, and not rely on the outer code to initialize it.

3) Double check of the right guess. First, you do it in guessLoop to print the right message, and then again in the main module. Again, all of this should be happening in guessLoop: something like this

def guessRun(rightAnswer):
counter=1
guess=getNextGuessFromUser("Please enter a number within the range: ")

guess = getNextGuessFromUser("<Too high message>")