Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need to recreate the Guess my Number program below by adding an ask_number() fun

ID: 3532726 • Letter: N

Question

need to recreate the Guess my Number program below by adding an ask_number() function and others functions so that it eventually the program's code is in a function called main(). I need comments explaining what I am doing!!! So I need both an ask_number() and main() function! Please help!


# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random

print(" Welcome to 'Guess My Number'!")
print(" I'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible. ")

# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop
while guess != the_number:
    if guess > the_number:
        print("Lower...")
    else:
        print("Higher...")
           
    guess = int(input("Take a guess: "))
    tries += 1

print("You guessed it! The number was", the_number)
print("And it only took you", tries, "tries! ")

input(" Press the enter key to exit.")

Explanation / Answer

# Guess My Number

#

# The computer picks a random number between 1 and 100

# The player tries to guess it and the computer lets

# the player know if the guess is too high, too low

# or right on the money


import random


#ask_number function

#asks the user for a number and returns the input


def ask_number():

return int(input("Take a guess: "))


#main function

#all program code in this function

def main():

print(" Welcome to 'Guess My Number'!")

print(" I'm thinking of a number between 1 and 100.")

print("Try to guess it in as few attempts as possible. ")


# set the initial values

the_number = random.randint(1, 100)

#call the ask_number function to get user input

guess = ask_number()

tries = 1


# guessing loop

while guess != the_number:

if guess > the_number:

print("Lower...")

else:

print("Higher...")


#call the ask_number function to get user input

guess = ask_number()

tries += 1


print("You guessed it! The number was " + str(the_number))

print("And it only took you " + str(tries)+ " tries! ")


#changed input to raw_input to make compatible with python 2.x

raw_input(" Press the enter key to exit.")


#standard boilerplate to call the main() function to begin

# the program.

if __name__ == '__main__':

main()