19.8k views
0 votes
**GIVING ALL POINTS** 4.02 Coding With Loops

I NEED THIS TO BE DONE FOR ME AS I DONT UNDERSTAND HOW TO DO IT. THANK YOU

Output: Your goal

You will complete a program that asks a user to guess a number.


Part 1: Review the Code

Review the code and locate the comments with missing lines (# Fill in missing code). Copy and paste the code into the Python IDLE. Use the IDLE to fill in the missing lines of code.


On the surface this program seems simple. Allow the player to keep guessing until he/she finds the secret number. But stop and think for a moment. You need a loop to keep running until the player gets the right answer.


Some things to think about as you write your loop:


The loop will only run if the comparison is true.

(e.g., 1 < 0 would not run as it is false but 5 != 10 would run as it is true)

What variables will you need to compare?

What comparison operator will you need to use?

# Heading (name, date, and short description) feel free to use multiple lines


def main():


# Initialize variables

numGuesses = 0

userGuess = -1

secretNum = 5


name = input("Hello! What is your name?")


# Fill in the missing LOOP here.

# This loop will need run until the player has guessed the secret number.


userGuess = int(input("Guess a number between 1 and 20: "))


numGuesses = numGuesses + 1


if (userGuess < secretNum):

print("You guessed " + str(userGuess) + ". Too low.")


if (userGuess > secretNum):

print("You guessed " + str(userGuess) + ". Too high.")


# Fill in missing PRINT statement here.

# Print a single message telling the player:

# That he/she guessed the secret number

# What the secret number was

# How many guesses it took


main()

Part 2: Test Your Code

Use the Python IDLE to test the program.

Using comments, type a heading that includes your name, today’s date, and a short description.

Run your program to ensure it is working properly. Fix any errors you observe.

Example of expected output: The output below is an example of the output from the Guess the Number game. Your specific results will vary based on the input you enter.


Output


Your guessed 10. Too high.

Your guessed 1. Too low.

Your guessed 3. Too low.

Good job, Jax! You guessed my number (5) in 3 tries!




When you've completed filling in your program code, save your work by selecting 'Save' in the Python IDLE.


When you submit your assignment, you will attach this Python file separately.


Part 3: Post Mortem Review (PMR)

Using complete sentences, respond to all the questions in the PMR chart.


Review Question Response

What was the purpose of your program?

How could your program be useful in the real world?

What is a problem you ran into, and how did you fix it?

Describe one thing you would do differently the next time you write a program.

User Anton
by
5.0k points

2 Answers

2 votes

Answer:

import random

from time import sleep as sleep

def main():

# Initialize random seed

random.seed()

# How many guesses the user has currently used

guesses = 0

# Declare minimum value to generate

minNumber = 1

# Declare maximum value to generate

maxNumber = 20

# Declare wait time variable

EventWaitTime = 10 # (Seconds)

# Incorrect config check

if (minNumber > maxNumber or maxNumber < minNumber):

print("Incorrect config values! minNumber is greater than maxNumber or maxNumber is smaller than minNumber!\\DEBUG INFO:\\minNumber = " + str(minNumber) + "\\maxNumber = " + str(maxNumber) + "\\Exiting in " + str(EventWaitTime) + " seconds...")

sleep(EventWaitTime)

exit(0)

# Generate Random Number

secretNumber = random.randint(minNumber, maxNumber)

# Declare user guess variable

userGuess = None

# Ask for name and get input

name = str(input("Hello! What is your name?\\"))

# Run this repeatedly until we want to stop (break)

while (True):

# Prompt user for input then ensure they put in an integer or something that can be interpreted as an integer

try:

userGuess = int(input("Guess a number between " + str(minNumber) + " and " + str(maxNumber) + ": "))

except ValueError:

print("ERROR: You didn't input a number! Please input a number!")

continue

# Increment guesses by 1

guesses += 1

# Check if number is lower, equal too, or higher

if (userGuess < secretNumber):

print("You guessed: " + str(userGuess) + ". Too low.\\")

continue

elif (userGuess == secretNumber):

break

elif (userGuess > secretNumber):

print("You guessed: " + str(userGuess) + ". Too high.\\")

continue

# This only runs when we use the 'break' statement to get out of the infinite true loop. So, print congrats, wait 5 seconds, exit.

print("Congratulations, " + name + "! You beat the game!\\The number was: " + str(secretNumber) + ".\\You beat the game in " + str(guesses) + " guesses!\\\\Think you can do better? Re-open the program!\\(Auto-exiting in 5 seconds)")

sleep(EventWaitTime)

exit(0)

if __name__ == '__main__':

main()

Step-by-step explanation:

User FlightPlan
by
4.3k points
6 votes

Answer:

numGuesses = 0

userGuess = -1

secretNum = 4

name = input("Hello! What is your name?")

while userGuess != secretNum:

userGuess = int(input("Guess a number between 1 and 20: "))

numGuesses = numGuesses + 1

if (userGuess < secretNum):

print(name + " guessed " + str(userGuess) + ". Too low.")

if (userGuess > secretNum):

print( name + " guessed " + str(userGuess) + ". Too high.")

if(userGuess == secretNum):

print("You guessed " + str(secretNum)+ ". Correct! "+"It took you " + str(numGuesses)+ " guesses. " + str(secretNum) + " was the right answer.")

Step-by-step explanation:

Review Question Response

What was the purpose of your program? The purpose of my program is to make a guessing game.

How could your program be useful in the real world? This can be useful in the real world for entertainment.

What is a problem you ran into, and how did you fix it? At first, I could not get the input to work. After this happened though, I switched to str, and it worked perfectly.

Describe one thing you would do differently the next time you write a program. I want to take it to the next level, like making froggy, tic tac toe, or connect 4

User Eezo
by
3.6k points