95.6k views
15 votes
You wrote a program to allow the user to guess a number. Complete the code to give the user feedback.

:
# Tell the user the guess was correct.
print("You were correct!")
keepGoing = False
else:
print("You were wrong.")

User DaraJ
by
4.5k points

2 Answers

14 votes

Answer:

The complete program is:

import random

keepGoing = True

num = random.randint(0,9)

guess = int(input("Your guess: "))

while keepGoing == True:

if num == guess:

print("You were correct!")

keepGoing = False

else:

print("You were wrong.")

guess = int(input("Your guess: "))

keepGoing = True

Step-by-step explanation:

To answer this program, I let the computer generate a random number, then let the user make a guess.

This imports the random module

import random

This initializes a Boolean variable keepGoing to True

keepGoing = True

Generate a random number

num = random.randint(0,9)

Let the user take a guess

guess = int(input("Your guess: "))

The loop is repeated until the user guesses right

while keepGoing == True:

Compare user input to the random number

if num == guess:

If they are the same, then the user guessed right

print("You were correct!")

Update keepGoing to False

keepGoing = False

If otherwise,

else:

then the user guessed wrong

print("You were wrong.")

Prompt the user for another number

guess = int(input("Your guess: "))

Update keepGoing to True

keepGoing = True

User Menztrual
by
4.8k points
8 votes

Answer:

Complete the code below, which is an alternative way to give the user the same hints.

if guess == correct:

# Tell the user the guess was correct.

print("You were correct!")

keepGoing = False

elif guess < correct

:

print("Guess higher.")

else

:

print("Guess lower.")

Step-by-step explanation:

Edge 2021

User Allan Mwesigwa
by
4.9k points