In this lab, you will write a function called numberGuessingGame(). When the function is called, it will do the following: generate a random integer between 1 and 10 (you will need to import and use the random library) allow user 3 chances to guess the number if the user guessed the correct number, print out "You guessed it!" and ends the game otherwise if user made an incorrect guess, print out "Incorrect!" it will compare the guess with the generated random number, and if the guess is smaller, print out a hint message "You guessed too low"; else if the guess is larger, print out a hint message "You guessed too high" reduce the chance by one printout how many chances are left if user has not used up the chances, ask the user to guess the number If user used up the three chances, print "You lost! The number is [number]". Game ends Sample output: >>> numberGuessingGame() Guess a number between 1 and 10: 8 You guessed too high You have 2 chances left. Guess a number between 1 and 10: 4 You guessed too high You have 1 chances left. Guess a number between 1 and 10: 3 You guessed too high You have 0 chances left. You lost! The number is 1 Check Point Thoroughly test your program and make sure your program works. Submit your python code as .py file in text format, and capture the screens of your program running showing multiple input and output.

Respuesta :

Answer:

#here is code in Python

# import library

import random

#function

def numberGuessingGame():

   # generate random number between 1-10

   num = random.randint(1,10)

   no_of_g = 3

 #loop to check the guess

   for i in range(no_of_g):

       #read inout number from user

       inp_num = int(input("Guess a number between 1-10:"))

       #check input is less than random number

       if inp_num < num:

           print("Your guess is too low")

           print("", 2-i, "chances left.")

           continue

       #check input is greater than random number

       elif inp_num > num:

           print("Your guess is too high")

           print("", 2-i, "chances left.")

           continue

       #check input is Equal random number

       else:

           print("You won!")

           print("The number is", num)

           break

   #if chances are Equal to 0

   if not (num == inp_num):

       print("You lost!")

       print("The number is", num)

#call the function

numberGuessingGame()

Explanation:

Generate a random number between 1-10.Then ask user to enter a number.If user's input is greater than random number then print "too high".If number is less than random number then print "too low".If user's input is equal to random number then print "You won!" and random number in new line. This will repeat for 3 time.If user is not able to guess the random number for 3 times then it will print "You lost!" and the random number.

Output:

Guess a number between 1-10:5

too high

2 chances left.

Guess a number between 1-10:6

too high

1 chances left.

Guess a number between 1-10:1

too low

0 chances left.

You lost!

The number is 3