0

So I am just trying to write some stuff in python and I need to make this code loop until "Yes" or "yes" is specified but it keeps breaking even when "Yes" or "yes" is not specified. Please help me fix this, and thanks in advance.

print("Please now take the time to fill in your DOB:")
DOB_day = raw_input ("Date of month:")
DOB_month = raw_input ("Month:")
DOB_year = raw_input ("Year:")

DOB_confirmation = raw_input ("Please confirm, is this correct?")


while DOB_confirmation != "No" or "no":
    DOB_day = raw_input ("Date of month:")
    DOB_month = raw_input ("Month:")
    DOB_year = raw_input ("Year:")
    DOB_confirmation = raw_input ("Please confirm, is this correct?")
    if DOB_confirmation == "Yes" or "yes":
        break
Kasey
  • 25
  • 1
  • 7
Ben
  • 3
  • 4

3 Answers3

1

Take a look at your while DOB_confirmation != "No" or "no": line. You're trying to say "While the confirmation answer isn't yes, keep asking for the birthday" ... but that's not what you wrote. You're also using or incorrectly.

Try this: while DOB_confirmation.lower() != "yes":. That actually says "While the user hasn't entered any form of 'YES'" which is what you're looking for.

You can eliminate the if statement at the end - it's covered by the while loop.

Try this:

print("Please now take the time to fill in your DOB:")
DOB_day = input("Date of month:")
DOB_month = input("Month:")
DOB_year = input("Year:")

DOB_confirmation = input("Please confirm, is this correct?")


while DOB_confirmation.lower() != "yes":
      DOB_day = input("Date of month:")
      DOB_month = input("Month:")
      DOB_year = input("Year:")
      DOB_confirmation = input("Please confirm, is this correct?")
Kasey
  • 25
  • 1
  • 7
0

Try running your code like this

print("Please now take the time to fill in your DOB:")

while True:
    DOB_day = raw_input("Date of month:")
    DOB_month = raw_input("Month:")
    DOB_year = raw_input("Year:")
    DOB_confirmation = raw_input ("Please confirm, is this correct? (Yes/No)")
    if DOB_confirmation.lower() == "yes":
        break

I like doing this type of loop, it's sort of a substitute for a do-while in java.

.lower() turns your string into lowercase. So if the user types in 'YES', or 'yEs", or etc.. it will read the string just as 'yes'.

You can also use .upper() to turn your string into uppercase. Hope this helps.

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
Ryno_XLI
  • 161
  • 9
0

raw_input is not defined on my version of Python (3.7.0), so I replaced it with regular input. Furthermore, once I indented everything, it appears to work fine, save for accepting 'YES'.

Code:

print("Please now take the time to fill in your DOB:")
DOB_day = input ("Date of month:")
DOB_month = input ("Month:")
DOB_year = input ("Year:")

DOB_confirmation = input ("Please confirm, is this correct?")


while DOB_confirmation != "No" or "no":
    DOB_day = input ("Date of month:")
    DOB_month = input ("Month:")
    DOB_year = input ("Year:")
    DOB_confirmation = input ("Please confirm, is this correct?")
    if DOB_confirmation == "Yes" or "yes":
        break

print("broke (the good way)")

output:

================= RESTART: C:/work/stackoverflow/laksjdfh.py =================
Please now take the time to fill in your DOB:
Date of month:asdf
Month:fasd
Year:3232
Please confirm, is this correct?q
Date of month:asdf
Month:fasd
Year:gggf
Please confirm, is this correct?YES
broke (the good way)
>>>
Gigaflop
  • 390
  • 1
  • 13