-2

After getting a bunch of undesired output(s) from my code I realised that I may have no clear understanding of for and while loops. My intention is to "nest" an if block in a while or for loop, where instead of exiting when the user inputs an unxpected answer it simple loops back (not infinitely)until the correct or at least desired input is taken in. Please help and possibly explain. Thank you.

employment_history = str(input("Have you ever been employed? Y/N: " ).title())
if employment_history == "Y":

    comp_name = str(input("What was the name of the company you worked for? ").title())
    ref_1 = str(input("Who was your reference at "+ comp_name +"? ").title())
    ref_1_num = input("What was "+ ref_1 + "'s number? ")
    ref_1_number = ref_1_num[0:3]+'-'+ ref_1_num[3:6]+'-'+ ref_1_num[6:10]
    print("The company you worked for was "+comp_name+", "+ ref_1 +" was your reference ", "their number was "+ ref_1_number +".")


elif employment_history == "N":

    print("No employment record entered.")

while employment_history != "Y" and "N":
    print("Please enter Y for 'YES' and N for 'NO'.")
    return
zerid N
  • 3
  • 1
  • What do you believe `while employment_history != "Y" and "N":` will do? Given that `"N"` will always be `"N"` and nothing else? – Jongware Apr 30 '20 at 21:32
  • 1
    `while employment_history != "Y" and "N"` is the wrong way to test a variable for multiple values. See this question https://stackoverflow.com/q/15112125/494134 – John Gordon Apr 30 '20 at 21:33
  • seeing as the user should give either Y or N as an answer, anything else should print the "Message prompting the user for Y or N". I realise I may have not placed the While statement in the correct order. – zerid N Apr 30 '20 at 21:38
  • 1
    The problem is that `and "N"` is treated as an entirely separate condition, and nonblank strings are always true, so the right half of the `and` statement will always be true. – John Gordon Apr 30 '20 at 21:42

2 Answers2

1

You could put the if statements inside the loop so that it reaches them every time it loops

while True:
    print("Please enter Y for 'YES' and N for 'NO'.")
    employment_history = str(input("Have you ever been employed? Y/N: " ).title())
    if employment_history == "Y":

        comp_name = str(input("What was the name of the company you worked for? ").title())
        ref_1 = str(input("Who was your reference at "+ comp_name +"? ").title())
        ref_1_num = input("What was "+ ref_1 + "'s number? ")
        ref_1_number = ref_1_num[0:3]+'-'+ ref_1_num[3:6]+'-'+ ref_1_num[6:10]
        print("The company you worked for was "+comp_name+", "+ ref_1 +" was your reference ", "their number was "+ ref_1_number +".")
        break

    elif employment_history == "N":
        print("No employment record entered.")

This code is an infinite loop, however, you have the if statement to check if requirements are met. If they are, then it goes through the code, and at the end of the statement it breaks the loop so it does not ask again.

Holden
  • 632
  • 4
  • 15
0
employment_history = None 

while employment_history != "N":

    employment_history = str(raw_input("Have you ever been employed? Y/N: " ))

    if employment_history == "Y":
        comp_name = str(raw_input("What was the name of the company you worked for? ").title())
        ref_1 = str(raw_input("Who was your reference at "+ comp_name +"? ").title())
        ref_1_num = raw_input("What was "+ ref_1 + "'s number? ")
        ref_1_number = ref_1_num[0:3]+'-'+ ref_1_num[3:6]+'-'+ ref_1_num[6:10]
        print("The company you worked for was "+comp_name+", "+ ref_1 +" was your reference ", "their number was "+ ref_1_number +".")

    elif employment_history == "N":
        print("No employment record entered.")
Employee
  • 3,109
  • 5
  • 31
  • 50