-1

I’m trying to get my game to tell the player how many chances they have left after every other turn. So for example, after the first try say “WARNING: you have x amount of tries left” and then “LAST CHANCE” when the final try comes.

I’m not sure if it’s possible to loop within a loop in the way that I’m trying to do it.

num = 2
guess = ''
guess_count = 0
guess_limit = 3
out_of_guesses = False

#make a loop to tell the player how many chances they have left

while guess != num and not(out_of_guesses):
  print("WARNING: You only get THREE chances")
  if guess_count < guess_limit :
    guess = int(input('Guess a number between 1 and 10: '))
    guess_count += 1
  else:
    out_of_guesses = True
    
if out_of_guesses:
  print('Out of guesses. LOSER!')
else:
  print('BINGO! The times the CHARM! WINNER!')

Thanks so much.

enter image description here

So I want the warning to change after every other turn unless the player guesses the correct number.

I hope this makes sense.

  • 1
    Does this answer your question? [How do I put a variable’s value inside a string (interpolate it into the string)?](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-s-value-inside-a-string-interpolate-it-into-the-string) The "variable" in question is the number of remaining guesses, which you can easily calculate from the variables you already know. – Pranav Hosangadi Feb 13 '23 at 18:50

2 Answers2

0

If you want the program to print how many guesses are left in the warning, you could do something like this:

print(f"WARNING: You have {guess_limit-guess_count} guesses left")

This uses a feature called 'f strings' which allows you to put the value of a variable inside a string. Here I'm using it to put the result of guess_limit - guess_count inside the string, which should resolve to the number of guesses remaining.

Rob Streeting
  • 1,675
  • 3
  • 16
  • 27
  • Please look for a duplicate before answering questions. Questions involving simple language features have usually already been asked and answered – Pranav Hosangadi Feb 13 '23 at 18:52
  • Thank you so much! How would I go about getting my code to tell the player to be careful once their last attempt comes? I want to add a completely different message. Thank you so much! – Uchenna Umeh Feb 13 '23 at 19:57
0
num = 2
guess = ''
guess_count = 3
guess_limit = 0
out_of_guesses = False

#make a loop to tell the player how many chances they have left

while guess != num and not(out_of_guesses):
  
  if guess_count > guess_limit and guess_count!=1:
    print("WARNING: You only get {} chances".format(guess_count))
    guess = int(input('Guess a number between 1 and 10: '))
    guess_count -= 1
  elif guess_count==1:
    print('LAST CHANCE:')  # different method for last chance
    guess = int(input('Guess a number between 1 and 10: '))
    guess_count -= 1
  else:
    out_of_guesses = True
    
if out_of_guesses:
  print('Out of guesses. LOSER!')
else:
  print('BINGO! The times the CHARM! WINNER!')


#output
WARNING: You only get 3 chances
Guess a number between 1 and 10: 4
WARNING: You only get 2 chances
Guess a number between 1 and 10: 8
LAST CHANCE:
Guess a number between 1 and 10: 6
Out of guesses. LOSER!

Document str.format()

https://python-reference.readthedocs.io/en/latest/docs/str/format.html

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • Instead of just dumping the fixed code, a good answer should _explain_ what was changed, and provide links to documentation and further reading. Please also look for a duplicate before answering questions. Questions involving simple language features have usually already been asked and answered – Pranav Hosangadi Feb 13 '23 at 18:54
  • sure Pranav, let me edit – Talha Tayyab Feb 13 '23 at 18:55