0

I am making a text-based game in Python, which I haven't touched in a while, for a class project. I want the game to use a name feature, and want the player to choose their name, so I put in a line of code that is supposed to allow them to repeat the line of code for entering their name. I am trying to use a loop for that, of course, but I don't know how to format it for an if/else statement. Here's the code I have:

def naming():
  print("A long, long time ago, there was a person who was born very ordinary, but would live to become very extraordinary.")
  time.sleep(3)
  name=input("Who are you? Give me your name.")
  choice=input("You said your name was ", name,", correct?")
  if choice in Yes:
    prologue():
  else:

I want to put the whole thing in a loop so that it repeats, (Which is why I haven't defined the "else" at the end) but I want the loop to break if the player says "yes" when prompted. Thanks for reading this far!

sloth
  • 99,095
  • 21
  • 171
  • 219
Rene707
  • 31
  • 5

2 Answers2

1
while True:
    name=input("Who are you? Give me your name.")
    choice = input(f'You said your name was {name}, correct?')
    if 'yes' in choice.casefold():
        break

Credits to devdev_dev

Rene707
  • 31
  • 5
  • you can select an answer as correct and then its featured at the top for other people to reference it, so you don't have to write another comment with the final answer – user7247147 Oct 07 '20 at 03:59
0

you can do something like

while True:
    name=input("Who are you? Give me your name.")
    choice = input(f'You said your name was {name}, correct?')
    if 'yes' in choice.casefold():
        break
user7247147
  • 1,045
  • 1
  • 10
  • 24
  • the choice is not a variable though, will that still work? – Rene707 Oct 07 '20 at 03:26
  • The answer is missing `name=input("Who are you? Give me your name.")` after `while True` – James Lin Oct 07 '20 at 03:34
  • `input` just takes 1 argument and its a string, so you interpolate the variable into the string – user7247147 Oct 07 '20 at 03:38
  • Will it allow the player to re-enter if they say no? – Rene707 Oct 07 '20 at 03:40
  • yes the block of code in the loop will repeat until the user puts yes - its an infinite loop that only ends when you get your desired output - while loops really useful for this scenario – user7247147 Oct 07 '20 at 03:43
  • Thank you very much. I am still entering it and double checking, I'll have the results updated shortly – Rene707 Oct 07 '20 at 03:46
  • Never mind, it doesn't work. It worked unless you say no. Then it uses 0, the default value, in place of the name variable and doesn't let you re-enter your name – Rene707 Oct 07 '20 at 04:08
  • I ended up using 'return' on the else statement and it works now. Thank you! – Rene707 Oct 07 '20 at 04:14