I have made a guessing game that looks like this
secret_number=9
guess_count=0
guess_limit=3
question_display_times=0
question_to_be_displayed=1
while question_display_times<question_to_be_displayed:
print(’’‘What two whole, positive numbers that have a one-digit answer
when multiplied and a two-digit answer when added?
The sum of the two numbers that you get is the answer
‘’’)
question_display_times=question_display_times+1
while guess_count<guess_limit:
guess=int(input(‘Guess the secret number if you can ‘))
if guess==secret_number:
print(’’‘Congratulations on winning this test
You are a true detective!’’’)
break
elif guess!=secret_number:
print(‘Try again’)
guess_count = guess_count + 1
else:
print(’’‘Sorry you failed ’’’)
Can anyone tell me that when I run the program and guess the secret number wrong it appears like this
What two whole, positive numbers that have a one-digit answer
when multiplied and a two-digit answer when added?
The sum of the two numbers that you get is the answer
Guess the secret number if you can 1
Try again
Guess the secret number if you can 1
Try again
Guess the secret number if you can 1
Try again
Sorry you failed
How can I remove this try again at the last so that after the third try it only shows Sorry you failed
Hello!
First, I won’t answer your question, but I’ll try to guide you to find the answer
The reason the “Try again” message is showing up is because it’s part of the ‘elif’, meaning that it will always show up BEFORE the "Sorry you failed " message.
Think of it this way: Once the user fails for the third time, execute some other code (The "Sorry you failed " message)
So, your main problem is that after the third attempt is failed, some other code must execute. Find a way to detect this, and your problem will be solved.
The hardest part of coding is to think in different ways… it takes time, and lots of practice. Before you write any line of code, take a piece of paper and a pen and write in simple words a way to solve the problem. This is: solve the problem in your head first… this way when you sit and code, everthing will be easier.
A quick suggestion on your code. The first while loop is doing nothing there. It prints only once, so why doing it in a loop?
secret_number=9
guess_count=0
guess_limit=3
question_display_times=0
question_to_be_displayed=1
while question_display_times<question_to_be_displayed:
print('What two whole, positive numbers that have a one-digit answer when multiplied and a two-digit answer when added?')
question_display_times=question_display_times+1
while guess_count<guess_limit:
guess=int(input('Guess the secret number if you can: '))
if guess==secret_number:
print('Congratulations on winning this test')
break
elif guess_count == guess_limit - 1:
print('Sorry you failed')
guess_count = guess_count + 1
elif guess!=secret_number:
print('Try again')
guess_count = guess_count + 1