You wrote a program to allow the user to guess a number. Complete the code.
while keepGoing:
guess = input("Guess an integer from 1 to 10: ")
guess = int(guess)
attempts = attempts + 1
if guess == correct:
print("You were correct!")
keepGoing = ______
else:
if guess < correct:
print("Guess higher.")
else:
print("Guess lower. ")

Respuesta :

Answer:

The complete program is as follows:

keepGoing = True

attempts = 0

correct = 2

while keepGoing:

    guess = input("Guess an integer from 1 to 10: ")

    guess = int(guess)

    attempts = attempts + 1

    if guess == correct:

         print("You were correct!")

         keepGoing = False

    elif guess < correct:

         print("Guess higher.")

    else:

         print("Guess lower. ")

Explanation:

This line initializes keepGoing to true

keepGoing = True

This initializes number of attempts to 0

attempts = 0

This initializes the correct guess to 2 (you can also use any number)

correct = 2

The following iteration is repeated until the number is guessed correctly

while keepGoing:

    guess = input("Guess an integer from 1 to 10: ")

    guess = int(guess)

    attempts = attempts + 1

    if guess == correct:

         print("You were correct!")

If user guess is correct, keepGoing is set to False

         keepGoing = False

    elif guess < correct:

         print("Guess higher.")

    else:

         print("Guess lower. ")

Following are the modified program to the given code:

Program Explanation:

  • Defining a "keepGoing" variable as bool type.
  • Defining an integer variable "correct".
  • Defining a while loop that use keepGoing variable to start the loop.
  • Inside the loop a "guess" variable is defined that input value from the user-end.
  • After input an if block is declared that check "guess" variable value equal to "correct" variable value.
  • When the condition is false it goes into the else block in which a conditional block is used that checks the range of input and print its value.    

Program:

keepGoing = True#defining a bool variable keepGoing

correct = 5#defining an integer variable correct

while keepGoing:#defining a while loop that use keepGoing variable to start the loop  

   guess = int(input("Guess an integer from 1 to 10: "))#defining a guess variable that input integer value from user-end

   if guess == correct:#defining if block that check guess variable value equal to correct variable value

       print("You were correct!")#print message

       keepGoing = False#defining bool variable that holds boolean value

   else:#defining else block

       if guess < correct:#defining another if block that checks the range of input

           print("Guess higher.")#print message

       else:#else block

           print("Guess lower.")#print message

  • For the same code please find the attached file.

Output:

Please find the attached file.

Learn more:

brainly.com/question/13664230

Ver imagen codiepienagoya
Ver imagen codiepienagoya
ACCESS MORE
EDU ACCESS