Python Forum
Having two inputs to escape a while loop.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having two inputs to escape a while loop.
#1
For homework, ive been set a task to create a program which repeatedly asks the user if they need help. The question is asked over and over until the user either enters 'yes' or 'Yes'. I have tried everything to get it working. I just cant seem to get it to work... I have used square brackets to make a list under one variable containing 'yes' and 'Yes' i have even subsituted them for normal brackets... Help me please!

Heres the code: Loops arent my forté but its my best shot: My Document

answer = ["yes","Yes"]
userinput = (" ")
while userinput != answer:
    userinput = input("Do you need help?")
print("Heres your work!")
Reply
#2
I wouldn't use a list here. I would recommend removing the first line. In the third line, i would replace answer with 'yes'. Then in the fourth line, you could use .lower() to make all types of 'yes' to be recognized by the test in the while loop, whether it is with lowercase or uppercase.
Something like this:

userinput = ('')
while userinput != 'yes':
    userinput = input("Do you need help? ").lower()
print("Heres your work!")
Reply
#3
When working with something that has more than one item (like answer) you can use in or not in to check if something is in or not in the sequence.
Reply
#4
Kotter's solution works as long as it is also acceptable to respond to yEs, which was not exactly the assignment as laid out. Try using Yoriz's suggestion.
Since your initial attempt used a list I am assuming that the instructor was using this to teach a bit about lists, in which case this works. If instead you were to learn about string case, Kotter's works.
Reply
#5
Hello everyone!
Thankyou so much for your input, both solutions have worked. I think that Yoriz's solution was more appropriate as we havenot been taught more ocmplicated stuff like the .lower() syntax's Once again, thankyou all and muc appreciated.

Both bits of code below worked!

#Program which repeatedly asks user if they need help until they say yes mwahahaha!
Y=["yes", "Yes"]
answer = ("")
while answer not in Y:
    answer = input("Do you have another question?\n")
print("OK")
-----------------------------------------------------

userinput = ("")
while userinput != 'yes':
    userinput = input("Do you need help?").lower()
print("Heres your work!")
I rewrote the code in the second one aswell. In my opinion it looks neater.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020