In: Computer Science
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number.
Requirements:
Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment statement to set the secret number to 1458.
Your program should start by welcoming the user and prompting the user to enter a guess:
Welcome to the guessing game. You have ten tries to guess my number. Please enter your guess:
You can assume that the user will enter an integer as a guess; however, your program must verify that each guess is positive and less than 10000. If it is not, it should print the following message and reprompt the user:
Your guess must be between 0001 and 9999. Please enter a valid guess:
It should repeat this until the user enters a valid guess.If the user has entered a correct guess, your program should print the message:
That's correct! Congratulations! You guessed it in X guesses.
where X is the appropriate number of tries. However, if the user guessed correctly on the first try, your program should print the message:
That's correct! Congratulations! You guessed it on the first try!
Your program should then terminate.If the user's guess is low, your program should print the message:
Your guess is too low. Guesses so far: X
where X is the number of tries so far.If the guess was the user's tenth guess, your program should print the message:
Game over: you ran out of guesses.
Your program should then terminate.If it was not the tenth guess, then your program should prompt the user for the next guess:
Please enter your guess:
If the user's guess is high, your program should print the message:
Your guess is too high. Guesses so far: X
where X is the number of tries so far.If the guess was the user's tenth guess, your program should print the message:
Game over: you ran out of guesses.
Your program should then terminate.If it was not the tenth guess, then your program should prompt the user for the next guess:
Please enter your guess:
Expected output:
This is what your output should look like if the user guessed the number on the first try:
Welcome to the guessing game. You have ten tries to guess my number. Please enter your guess: 1458 That's correct! Congratulations! You guessed it on the first try! >>>
This is what your output might look like if the user guessed the number within ten tries:
Welcome to the guessing game. You have ten tries to guess my number. Please enter your guess: 5000 Your guess is too high. Guesses so far: 1 Please enter your guess: 2500 Your guess is too high. Guesses so far: 2 Please enter your guess: 1250 Your guess is too low. Guesses so far: 3 Please enter your guess: 1800 Your guess is too high. Guesses so far: 4 Please enter your guess: 1500 Your guess is too high. Guesses so far: 5 Please enter your guess: 1400 Your guess is too low. Guesses so far: 6 Please enter your guess: 1450 Your guess is too low. Guesses so far: 7 Please enter your guess: 1460 Your guess is too high. Guesses so far: 8 Please enter your guess: 1458 That's correct! Congratulations! You guessed it in 9 guesses. >>>
This is what your output might look like if the user failed to guess the number within ten tries:
Welcome to the guessing game. You have ten tries to guess my number. Please enter your guess: 5000 Your guess is too high. Guesses so far: 1 Please enter your guess: 2500 Your guess is too high. Guesses so far: 2 Please enter your guess: 1200 Your guess is too low. Guesses so far: 3 Please enter your guess: 1800 Your guess is too high. Guesses so far: 4 Please enter your guess: 1500 Your guess is too high. Guesses so far: 5 Please enter your guess: 1350 Your guess is too low. Guesses so far: 6 Please enter your guess: 1375 Your guess is too low. Guesses so far: 7 Please enter your guess: 1400 Your guess is too low. Guesses so far: 8 Please enter your guess: 1425 Your guess is too low. Guesses so far: 9 Please enter your guess: 1450 Your guess is too low. Guesses so far: 10 Game over: you ran out of guesses. >>>
This is what your output might look like if the user entered a few invalid guesses:
Welcome to the guessing game. You have ten tries to guess my number. Please enter your guess: 20000 Your guess must be between 0001 and 9999. Please enter a valid guess: 5000 Your guess is too high. Guesses so far: 1 Please enter your guess: -2500 Your guess must be between 0001 and 9999. Please enter a valid guess: -1200 Your guess must be between 0001 and 9999. Please enter a valid guess: 1 Your guess is too low. Guesses so far: 2 Please enter your guess: 1000 Your guess is too low. Guesses so far: 3 Please enter your guess: 1400 Your guess is too low. Guesses so far: 4 Please enter your guess: 1500 Your guess is too high. Guesses so far: 5 Please enter your guess: 1450 Your guess is too low. Guesses so far: 6 Please enter your guess: 1460 Your guess is too high. Guesses so far: 7 Please enter your guess: 1458 That's correct! Congratulations! You guessed it in 8 guesses. >>>
#number to be guessed
num=1458
print("Welcome to the guessing game. You have ten tries to guess my number.")
#input x(guess)
x=int(input("Please enter your guess: "))
#number of guesses
count=1
while(count<=10):
# case for guess in first attempt
if x==num and count==1:
print("That's correct!\nCongratulations! You guessed it on the first try!")
break
# case for guess in attempt less than or equal to 10
elif x==num:
print("That's correct!\nCongratulations! You guessed it in",count,"guesses.")
break
# case for invalid input
elif x<=0 or x>=10000:
print("Your guess must be between 0001 and 9999.")
x=int(input("Please enter a valid guess: "))
# case for low guess and count
elif x>num:
print("Your guess is too high.\nGuesses so far: ",count)
#when 10 guesses are complete
if count==10:
print("Game over: you ran out of guesses.")
break
x=int(input("Please enter your guess: "))
count=count+1
# case for low guess and count
elif x<num:
print("Your guess is too low.\nGuesses so far: ",count)
#when 10 guesses are complete
if count==10:
print("Game over: you ran out of guesses.")
break
x=int(input("Please enter your guess: "))
count=count+1
#end
OUTPUTS:
1
2