Question
python program
Problem 3 (Taking User Input) Write a function called userGuess(n) that takes an integer n as an argument. This function shou

sample output
Sample Output (user input is highlighted) Welcome to Power Quiz Game Type stop to exit Score: What number is the largest po
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screenshot:

1e import random 2 import math 3 # Create a function userGuess takes n as an argument 4 def userGuess(n): 50 # display n to t

# if both are not equal else: # Display wrong message and correct number print(Wrong! The correct answer is + str(int(math

Sample Output:

Welcome to Power Quiz Game What is the largest power of 2 that is less than or equal to 488?256 Correct! Score: 1 What is the

Program Code:

import random
import math
# Create a function userGuess takes n as an argument
def userGuess(n):
   # display n to the user and
   #prompt the user to enter largest power of 2 that is less than or equal to n
   num = input("What is the largest power of 2 that is less than or equal to " +str(n) + "?")
   # return user input num
   return num
#create a main function
def main():
   # display welcome message
   print("Welcome to Power Quiz Game")
   # declare and initialize num
   num = ""
   # declare and initialize score
   score = 0
   # prompt the user to enter input until user enters stop
   while(num != "stop"):
       # generate a random integer n in the range[0, 4096]
       n = random.randrange(0,4096)
       # call the function userGuess to prompt number
       num = userGuess(n)
       # if user enters stop
       if(str(num) == "stop"):
           # display the score and stop
           print("Your final score is " + str(score))
           break
       # calculate the largest power of 2(random number)
       power = int(math.log(int(n), 2));
       # if user entered number and largest power of 2 is equal then display the score and message
       if(int(num) == int(math.pow(2, power))):
           # increase the score if both are equal
           score = score + 1
           # display correct message
           print("Correct!")
           # display the score
           print("Score: " + str(score))
       # if both are not equal
       else:
           # Display wrong message and correct number
           print("Wrong! The correct answer is " + str(int(math.pow(2, power))))
# Call main function  
main()

Add a comment
Know the answer?
Add Answer to:
python program sample output Problem 3 (Taking User Input) Write a function called userGuess(n) that takes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • Write a Java program that prompts the user for the page size used in a virtual...

    Write a Java program that prompts the user for the page size used in a virtual memory system; this will be a power of two between 512 (29) and 16384 (214), inclusive. Your program should check the user input for page size to make sure it is one of the allowable inputs (must be a power of 2 and cannot be smaller than 512 or larger than 16384), and should then prompt the user for a virtual address (assume 32-bit...

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4...

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. 1 largest = None 2 smallest = None 3 while True: 4 num...

  • Write a python program that repeatedly asks the user to input a pair of integers. The...

    Write a python program that repeatedly asks the user to input a pair of integers. The program should record the largest number of each pair into a list. The program should keep asking the user to input pairs of numbers until the user enters -1 for the first number. At this point the program should print the list on a single line, with each value separated by a space and then stop. Example of expected behaviour: Please enter first number:...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

  • Write a program thet uses the function strcmp() to compare two strings input by the user....

    Write a program thet uses the function strcmp() to compare two strings input by the user. The program should state whether the first string is less then, equal to, or greater then the second string 7. Write a program thet uses the function strcmp() to compare two strings input by the user. The program should state whether the first string is less then, equal to, or greater then the second string

  • (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the...

    (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the four basic arithmetic operations, i.e., addition, subtraction, multiplication and integer division. A sample partial output of the math quiz program is shown below. The user can select the type of math operations that he/she would like to proceed with. Once a choice (i.e., menu option index) is entered, the program generates a question and asks the user for an answer. A sample partial output...

  • Using Python: Write a program that takes the size of a file (in bytes) as input...

    Using Python: Write a program that takes the size of a file (in bytes) as input from the user, coverts it to the nearest whole number binary prefix (B for byte, KB for kilobyte, MB for megabyte, GB for gigabtye), rounded down. Any file sizes greater than 1024 GB should just be converted to GB.

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT