0

Trying to stop the below code from continuously requesting input after user enter q. I enter q as the first input but loop just continues. How do I get it while loop to stop if the user enter the letter q. I'm using python3 Thank you.

w = ''
while w != 'q':
    w = input("Enter the student's W#:")
    name = input("Enter the student's name:")
    phone = input("Enter the students phone number:")
1
  • 4
    while True: w= input() if w=='q': break Commented Jun 30, 2022 at 20:15

3 Answers 3

1

Every time the loop has to run through all the way. The only way to stop is to check during the loop if it q has been typed. Eg:

while true:
    w = input("Enter the student's W#:")
    if (w == 'q') 
        break
    name = input("Enter the student's name:")
    if (name == 'q') 
        break
    phone = input("Enter the students phone number:")
    if (phone == 'q') 
        break
Sign up to request clarification or add additional context in comments.

Comments

1

Check the input after they enter it, not when the loop repeats.

while True:
    w = input("Enter the student's W# or q to end:")
    if w == 'q':
        break
    name = input("Enter the student's name:")
    phone = input("Enter the student's phone number:")

Comments

0

When you want to exit a loop, you can insert break.

w = ''
while w != 'q':
    w = input("Enter the student's W#:")
    if w == 'q': break
    name = input("Enter the student's name:")
    phone = input("Enter the students phone number:")

Given the test your loop employs, you could also continue skipping to the next iteration.

w = ''
while w != 'q':
    w = input("Enter the student's W#:")
    if w == 'q': continue
    name = input("Enter the student's name:")
    phone = input("Enter the students phone number:")

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.