Question

In: Computer Science

Write a program that generates a random number in the range of 1 through 100, and...

Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even.

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.” Keep asking the user till the correct number is guessed. If the user enters 0 for the guess, the game terminates.

When the user successfully guesses the number, the application should congratulate the user and then display how many tries it took the user to guess the number. Moreover, display all the odd or even numbers starting at 1, up to that number. (Display all odd numbers if the number itself was odd, or all even numbers if the number was even). For example, assume that 16 was the generated number. The final display should be:

You took x tries to guess the number.

16 is an even number.

All even numbers up to 16 are: 1, 2, 4, 6, 8, 10, 12, 14

To generate the random number, you need to import the random library   (import random) and use the randint function ( random.randint(1, 100) )

Requirements

  • Make all the "best programming practice" decisions, e.g. how to show the output, what prompts to display, how to ask for input etc.
  • You are the developer/engineer, it is YOUR decision ... YOUR job. If the program is not presented nicely, you will lose points.
  • First, all that is being asked should be done. Second, the displays should be intuitive, self-explanatory and nicely put.
    Do NOT assume that the user knows ANYTHING.
  • Write the program such that any new person that sits in front of the terminal, can start playing with it (i.e., the commands, displays etc. are adequate).

Solutions

Expert Solution

Python Program:

""" Python program that generates Guessing Game """

import random

# Generating a random number in 1 to 100
actual = random.randint(1, 100)

# Checking for even or odd
if actual%2 == 0:
   print("\nNumber generated is an even number.\n")
   even = True
else:
   print("\nNumber generated is an odd number.\n")
   even = False
  
# Variable to hold number of guesses
numGuesses = 0  

# Playing game
while True:
   # Prompting user
   guess = int(input("\nYour Guess: "))
   # Incrementing number of Guesses
   numGuesses += 1
   # Checking guess
   if guess == actual:
       break;
   elif guess > actual:
       # Printing message to user
       print("Too high, try again.")
   elif guess < actual:
       # Printing message to user
       print("Too low, try again.")
  
# Printing number of tries
print("\nYou took " + str(numGuesses )+ " tries to guess the number.\n")

# List to hold values
vals = []

# Checking even or odd
if even:
   print(str(actual) + " is an even number.")
   print("All even numbers up to " + str(actual) + " are: ", end="")
   # Iterating over each value
   for i in range(1, actual):
       # Checking only for even numbers
       if i%2==0:
           vals.append(str(i))
else:
   print(str(actual) + " is an odd number.")
   print("All odd numbers up to " + str(actual) + " are: ", end="")
   # Iterating over each value
   for i in range(actual):
       # Checking only for even numbers
       if i%2!=0:
           vals.append(str(i))
# Printing result          
print(','.join(vals))

_______________________________________________________________________________________

Code Screenshot:

_______________________________________________________________________________________

Sample Run:


Related Solutions

Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program 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. Your program should also keep a...
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
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....
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 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. Count the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT