python 3program hello i need help with this code in python o
python 3program
hello
i need help with this code in python only please. i wrote one but it is not working and having hard time debugging. as you can see below this has only 2 functions and uses the random.randint functions as well. please post an answer that would be easy to understand since i am only trying to fix my code not blindly copy an answer! thank you!! ps: i have posted this yesterday and got the wrong answer and no explanation which is not what i am looking for:(
You are tasked with developing a program that gets a random number, and then tries to guess it. The program should have a main() function that prompts a user for input that will be used as the seed to a random number generator. This main() function should then generate a random number between 1 and 100 inclusive and pass it to a function called randomGuess(r) where r is an integer. The randomGuess() function should then attempt to guess the random number argument. This should be accomplished by having the function initially guess a number from 1-100 where each guess is printed out. If the guess is correct, print a message. Otherwise, the program should determine if it is a high or low guess. The subsequent guess(es) should restrict the range based on this feedback. For instance, if the randomly chosen number is 33 and the initial guess is 50, the program should then limit the next guess to be between 1-49. Continuing, if the next guess is 10, the next guess would be restricted from 11-49. These guesses will continue until the number is guessed correctly.
Solution
import random
def main():
r = input(\"Enter a number between 0 to 100\") # Get input from user
randomGuess(r)
def randomGuess(r):
l = 0 # set the lower limit to zero
h = 100 # set the higher limit to 100
n = random.randrange(l,h) # generate a random number between 0,100
while(r!=n): # guess untill random equals number
if(n>r): # if guess is greater than number
print \"Guess is high\",n,l,h
h = n # change the higher limit to guessed number
if(n<r): # if guess is less than number
print \"Guess is low\",n,l,h
l = n # changed the lower limit to guessed number
n = random.randrange(l,h) # generate random number between l and h.
print \"Guess is correct\"
main()
#sample output
