Question

In: Computer Science

(python only) Assignment: Guess a number To get started, open IDLE and create a New File...

(python only)

Assignment: Guess a number

To get started, open IDLE and create a New File via the File menu. We suggest you immediately save this file in the directory managing all your 102 Python Labs this semester. Please save this file with the following name: Week9A-guess_number.py.

In this lab, use while loops to create a game where the user of your program guesses a number between 1 and 100. The user keeps guessing until they get it right. You should structure your program as follows:

  • Generate a random number between 1 and 100 that the user will guess.

NOTE: You must use a seed with your random number generator. This will make the grader's life WAY easier.

print("Number to initialize the random generator:")
my_seed = int(input("SEED> "))
random.seed(my_seed)

  • Ask the user to guess a number:
    • If the number is greater than 100, ask the user to input a number between 1 and 100
    • If the number is less than 1, ask the user to input a number between 1 and 100
    • If the number is 50 or greater from the randomly generated number, print out "You're cold!"
    • If the number is 25 or greater and less than 50 from the randomly generated number, print out "You're lukewarm!"
    • If the number is 15 or greater and less than 25 from the randomly generated number, print out "You're getting warm!"
    • If the number is 5 or greater and less than 15 from the randomly generated number, print out "You're getting hot!"
    • If the number is less than 5 from the randomly generated number, print out "You're so close!"
    • When the user chooses the correct number, print out "Congrats! You won!"
  • Repeat until the user guesses the correct number.

Sample Execution

Number to initialize the random generator: 32

Enter a number between 1 and 100: 95
OUTPUT You're cold!

Enter a number between 1 and 100: 105
OUTPUT Please Enter a number between 1 and 100

Enter a number between 1 and 100: 41
OUTPUT You're lukewarm!

Enter a number between 1 and 100: 32
OUTPUT You're getting warm!

Enter a number between 1 and 100: 16
OUTPUT You're getting hot!

Enter a number between 1 and 100: 9
OUTPUT You're so close!

Enter a number between 1 and 100: 10
OUTPUT Congrats! You won!

Solutions

Expert Solution

Thanks for the question, here is the code in Python.

======================================================================

import random
def main():

    seed = int(input('Number to initialize the random generator: '))
    random.seed(seed)

    secret_number = random.randint(1,100)
    user_number=-1
    while user_number!=secret_number:
        user_number = int(input('\nEnter a number between 1 and 100: '))
        if user_number<1 or user_number>100:print('Please Enter a number between 1 and 100')
        elif abs(secret_number-user_number)>=50:print('You\'re cold!')
        elif abs(secret_number-user_number)>=25:print('You\'re lukewarm!')
        elif abs(secret_number-user_number)>=15:print('You\'re getting warm!')
        elif abs(secret_number-user_number)>=5:print('You\'re getting hot!')
        elif 0<abs(secret_number-user_number)<5:print('You\'re so close!')
        elif user_number==secret_number:print('Congrats! You won!')

main()

======================================================================

Code screenshot


Related Solutions

In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the...
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the code below in the new file (you may omit the comments, I included them for explanation #Python Code Begin x = int(input("Enter a number: ")) y = int(input("Enter another number: ")) print ("Values before", "x:", x, "y:", y) #add code to swap variables here #you may not use Python libraries or built in swap functions #you must use only the operators you have learned...
Create a Guess the Number game. Python randomly chooses a secret four-digit number (with no repeating...
Create a Guess the Number game. Python randomly chooses a secret four-digit number (with no repeating digits) and asks the user to guess it. The user has up to ten attempts to guess the number. After each guess, Python gives the user two clues (until the user either guesses the number correctly or runs out of attempts): The number of digit(s) in the user’s guess that is (are) both correct and in the right position. The number of digit(s) in...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program:...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program: Your program will create a username of your choice using a Kali Linux command "useradd -m mark", then set the password for that user using the Kali Linux Command "echo "mark:10101111" | chpasswd". Then you create a dictionary file using the Kali Linux command "crunch 8 8 01 > mylist.txt" Your python script should crack the password for that user and display on the...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
Java assignment Get a file name fname for output • Get number of data (numbers) (N)...
Java assignment Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Write the numbers into the file fname
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is...
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is provided from the standard input (keyboard) – the name is requested until it is correct Create and test the openFileWriteRobust () function to open file (for writing) whose name is provided from the standard input (keyboard) – the name is requested until is correct Extend the program to count number of characters, words and lines in a file as discussed in the classby including...
Using the IDLE development environment, create a Python script named tryme4.py. (Note: an alternative to IDLE...
Using the IDLE development environment, create a Python script named tryme4.py. (Note: an alternative to IDLE is to use a free account on the pythonanywhere website: https://www.pythonanywhere.com/) IDLE has both an interactive mode and a script mode. You must use the script mode to develop your script. Your script must use meaningful variable names and have comments that describe what is happening in your script. Comments may describe the assignment of a value to a variable, a computation and the...
In Python please:  Number the Lines in a File: Create a program that adds line numbers to...
In Python please:  Number the Lines in a File: Create a program that adds line numbers to a file. The name of the input file will be read from the user, as will the name of the new file that your program will create. Each line in the output file should begin with the line number, followed by a colon and a space, followed by the line from the input file.
I need to implement a code in python to guess a random number. The number must...
I need to implement a code in python to guess a random number. The number must be an integer between 1 and 50 inclusive, using the module random function randint. The user will have four options to guess the number. For each one of the failed attempts, the program it should let the user know whether is with a lower or higher number and how many tries they have left. If the user guess the number, the program must congratulate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT