123k views
3 votes
Pls answer this fellow coders i need it so bad

# 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 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


fill in the missing code

User Tsaarni
by
4.5k points

1 Answer

3 votes

This is how I would do it using the main function:

def main():

num_guesses = 0

secret_num = 5

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

while True:

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

if user_guess == secret_num:

num_guesses += 1

break

elif user_guess < secret_num:

num_guesses += 1

print("You guessed {}. That was too low!".format(user_guess))

elif user_guess > secret_num:

num_guesses += 1

print("You guessed {}. That was too high!".format(user_guess))

print("The secret number was {}. You guessed it correctly in {} tries".format(secret_num, num_guesses))

if __name__ == "__main__":

main()

I hope this helps!

User Nick Wills
by
3.7k points