Home > front end >  How do I get this to loop back to the start?
How do I get this to loop back to the start?

Time:02-28

def intro():
    print ("Welcome to Challenger!")
    print ("I am the friendly Merchant of many trades.")
    yourName = input("Your name is... ")
    print (("Hello, ")   yourName.upper()   (" my name is Nara, I am humble merchant at your service!"))
    print ("I have a quest for you, my friend.")
    print ("If you choose to accept?")
    response = input ("")
#game loops if the response doesnt start with y, Y, n or N

    if response.startswith("Y") or response.startswith("y") is False:
        print ("Great!")
        print ("I have three quests for you my friend- Choose wisely...")
    if response.startswith ("n") or response.startswith ("N") is True:
        print ("If you aren't here for a quest, what are you here for?")
        print ("...")
        print ("Well go then?")
        print ("")
    else:
        print ("Please enter a valid response- I don’t like my time being wasted.")
        print ("")
        intro()

I need some help with getting the code to loop back to the beginning only when response doesn't start with y, Y, N or n.

CodePudding user response:

Use a while loop?

def intro():
    print ("Welcome to Challenger!")
    print ("I am the friendly Merchant of many trades.")
    yourName = input("Your name is... ")
    print (("Hello, ")   yourName.upper()   (" my name is Nara, I am humble merchant at your service!"))
    print ("I have a quest for you, my friend.")
    print ("Do you choose to accept?")
    response = input ("")
    while not response.lower().startswith('y') and not response.lower().startswith('n'):
        print('Do not waste my time!')
        print("Do you choose to accept?")
        response = input("")
    if response.startswith("Y") or response.startswith("y"):
        print("Great!")
        print("I have three quests for you my friend- Choose wisely...")
    if response.startswith("n") or response.startswith("N"):
        print("If you aren't here for a quest, what are you here for?")
        print("...")
        print("Well go then?")
        print("")

CodePudding user response:

while True:
    print('welcome to Challenger')
    print('I am the friendly Merchant of many trades.')
    your_name = input('Your name is')
    print(('hello,')   your_name.upper() ('my name is Nara, I am humblemerchant at your service!'))
    print('i have a Quest for you, my friend.')
    print('if you choose to aacept?')
    response = input('')

    if response.startswith('y') or response.startswith('Y') is True:
       print('Great')
       print('I have three quests for ypu my friend choose Wisely')
       break

    if response.startswith("n") or response.startswith("N") is True:
       print('What are u here for?')
       print('...')
       print('Well go there')
       print('')
       break

    else:
       print('Please enter a valid reason')
       print('')
 

CodePudding user response:

Your code should be in while loop and the main game loop will be in a function. Like If answer == yes: Gameloop() Else: Print("by")

  • Related