-1

I am trying to loop back to the beginning of this code after it is done. After option 'C' is pressed, the program should end, (which I am also struggling with). But I want the program to loop to the beginning unless C is pressed. If 'A' or 'B' is pressed, the program should ask the user to enter an option again. This question is different as it has a specific example. I tried looking on Stack Overflow and found similar questions and tried them, but none of them worked out? Not sure why

    #input your full name
    def startagain():
     firstName=raw_input("Enter your first name: ")
     middleName=raw_input("Enter your middle name: ")
    lastName=raw_input("Enter your last name: ")
     #select what option you want.
    options=str(raw_input("Type 'A' for printing the length of your name, 
    'B' for printing your initials and 'C' to exit"))
     #prints first, middle and last name

    if (options == "A" or options == "a"):
         print firstName + " " + middleName + " " + lastName

    #prints your initials
  elif (options == "B" or options == "b"):
         print firstName[0] + "." + middleName[0] + "." + lastName[0] + "."
    #exit
   elif (options == "C" or options == "c"):
       exit()
         print ("OK, bye!")
    #invalid selection
    else:
        print "Invalid selection. Please select A, B, or C."


    if options == "A" or options == "B":
    startagain()

thanks in advance :)

1
  • 1
    All the indentation seems completely messed up (if I attempt to run your code I don't get past the first line). You also seem to have one function to gather user details, present a menu and operate on your menu selection. Perhaps breaking these into separate functions will help you determine where the error lies Commented May 4, 2018 at 1:00

2 Answers 2

0

remove call to startagain() at end, and put all the code inside a

while True:

loop.

Sign up to request clarification or add additional context in comments.

Comments

0

This is a common type of program and usually has this structure

while True:
    # print instructions for user
    # get his input
    # have a series of if/elif for handling each option
    # the option that indicates exit just does a break statement
    # to break out of loop

This addresses your question about looping but there are other issues with your code. If you are a beginner, you should try to spend time and debug your program, and also take a incremental approach to building your program

  1. get it working for one time without the loop
  2. build up the code a little at a time and make sure each part is working before adding more line
  3. perhaps add only one option at a time
  4. finally add the loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.