0

I want to ask the user if they want to play again and if they do, the code should repeat. I'm not sure how though. This is my code:

import random
import time

seconds = input ("How many seconds?")
seconds = int(seconds)

a_list = input('What items do you want? haikyu/mha/demon slayer')

if a_list == 'haikyu':
 items = ['hinata' 'kageyama', 'tsukishima', 'bokuto', 'kuroo', 'akaashi']

if a_list == 'mha':
 items = ['denki', 'bakugo', 'kirishima', 'deku', 'todoroki', 'tsu']

if a_list == 'demon slayer':
 items = ['tanjiro', 'nezuko', 'inosuke', 'zenitsu', 'tomioka']
 random.shuffle(items)

for item in items:
 print (item)

time.sleep(seconds)
random.shuffle(items)

removed_item = items.pop()

for item in items:
print(item)
guess = ''
while guess != removed_item:
guess = input('what is the missing item?')
martineau
  • 119,623
  • 25
  • 170
  • 301
eee
  • 1
  • Answer would be very similar as those to [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – martineau Dec 15 '21 at 21:11

2 Answers2

1

Put all of that in a function. Then, your main code just do:

while True:
    game()
    if input("Do you want to play again? (y/n): ") == "n":
        break
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

Your indentation seems to be quite off, to answer your question you can add another condition to your final lines:

while (guess != removed_item) and (retry != "No"):
   retry = input("Would you like to try again? Y/N")
   if retry == "Y":
      guess = input('what is the missing item?')
   else:
      break
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53