Question
Hello, I need help creating a function that will keep track of the lowest number of guesses since the user started the program . In
Hello, I need help creating a function that will keep track of the lowest number of guesses since the user started the program . In Python. Included below is my code so far.
import random
##Main Function def main(): number = random_num() name =get_name() game(name,number) ##Requests User's name and returns it def get_name(): name = input('Hello, what\'s your name?: ') return name
##Function creates and returns a random number def random_num(): number = 0 number = random.randint(1,100) return number
##Function takes the random number and creates a guessing game with it def game(name,number): cont = '' guessesTaken = 0 guesses = 999999 print('Good day to you,',name,' are you ready for a guessing game?!') ##Requests input if the user would like to continue cont = input('Would you like to play?(Type Yes or yes, No to exit) ')
##While loop for when the user wishes to continue while cont =='yes' or cont =='Yes': print('I\'m thinking of a number from 0-100') ##Requests user's guess and stores it guess = int(input('Pick your number: ')) while guess !=number:
##Too small if guess < number: print('Your guess is too low.') guessesTaken+= 1 guess = int(input('Pick your number: '))
##Too large elif guess > number: print('Your guess is too high.') guessesTaken+= 1 guess = int(input('Pick your number: '))
##Correct guesses that are even if guess == number and number%2==0: guessesTaken+= 1 print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!', 'It is an even number') if guessesTaken main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started