Question

In: Computer Science

Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...

Random Number Guessing Game

(python)

*use comments

Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the user and generate a new random number so the game can start over. If the user enters 0, they can exit out of the game.

Do not use break or exit() or continue etc.

Use the following code to have the computer pick a random number between 1 and 20 (inclusive):

import random # Place this line at the top of your program

#Place the line below at the appropriate place in your program:

number = random.randint(1, 20) # Generates random number from 1 to 20

Write a value returning function playGuessingGame(number) that takes in the number that the computer randomly generated as a parameter. It should ask for the user's input by asking 'Enter a number between 1 and 20, or 0 to quit:' , see if the guess is correct, and return the user's guess. If the user enters the correct number the function should congratulate them with 'Congratulations! You guessed the right number!'. The function should return the user's guess (even if they enter zero). (You do not need to worry about invalid values.)

Write a main function that calls the playGuessingGame method and keeps looping through until the user enters zero. Once they exit out of the game print 'Thanks for playing!'

Solutions

Expert Solution

#Feel free to ask queries!/ This program is successfully compiled and tested in Python 3.6 version
import random

def playGuessingGame(number):
    
    guess=int(input("Enter a number between 1 and 20,0 to quit"))
    if guess < number and guess != 0:
       print("Too low,Try again")
    elif guess > number:
        print("Too high,Try again")
    elif guess == number:
        print("Congatulations!")
        num=random.randint(1,20)# If the guess is correct then new random number is generated to start another game/
    return guess
    
#The main function is used to keep track of loop until the user enter "0" and if not then compiler calls the playGuessingGame function
def main():
    
    var=1    
    num=random.randint(1,20)
    while var:
        var=playGuessingGame(num) #The return value of playGuessingGame function is stored in var
    print("Thanks for playing!")
    
if __name__ == "__main__":
    main()   #This is used to call the main() function 

Related Solutions

Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Write a Python program which generates a random number between 0 and 24 inclusive and that...
Write a Python program which generates a random number between 0 and 24 inclusive and that print out: • the double of the random number if the random number is greater than 12, • the triple of the random number if it is equal to 12 • and the quadruple of the random number if it is less than 12
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 rand = random.randint(min_number, max_number) #Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") while (True): #While loop, comparing the users guessed number with the random number. #If it matches, it will say you guessed it. guess = eval(input("Please try to guess my...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT