-1

This is what I'm working just a little yes/no question.

print("Hello")
print("I love you")
answer = input("Do you love me?")
if answer in ["yes"]:
  print("Yey! I knew it! Thank You!")
if answer in ["no"]:
  print("O-ok, I understand, no worries.")


while True:
  try:
    print("Please answer me.")
    answer = input("Do you love me?")
  except ValueError:
   continue
  if answer in ["yes", "no"]:
    break

When I enter a valid answer it print what I want but also jumps into the loop asking again, if I answer again then it breaks. I don't understand why tho. When I enter an invalid answer it loops until it gets one right, when it gets one right it breaks but doesn't answer. Can someone please explain why?

Here is where I have been currently testing it: https://trinket.io/python/5a25117b84

  • Because the code where you answer is outside of your loop ? – LoicM Mar 29 '17 at 08:49
  • 1
    In the first block of code you ask the question once. In the second block of code, the loop, you ask the question until you get "yes" or "no", and then you break out of the loop, and nothing else. If you want it to print something, then you need some print statements in or after that second block. – khelwood Mar 29 '17 at 08:49
  • I don't see how you could expect it to do anything else. What do you *expect* the code to do? (Alternatively, what do you want it to do?) – Martin Bonner supports Monica Mar 29 '17 at 08:51
  • I think you need to keep reviewing whatever tutorial you're using. – TigerhawkT3 Mar 29 '17 at 08:53

2 Answers2

1

The position of while loop is not right. If you want to ask again if answer is invalid, try like this:

print("Hello")
print("I love you")
answer = input("Do you love me?")

while True:
    try:
        if answer == "yes":
            print("Yey! I knew it! Thank You!")
            break
        elif answer == "no":
            print("O-ok, I understand, no worries.")
            break
        else:
            print("Please answer me.")
            answer = input("Do you love me?")
    except ValueError:
        continue
Ni3_k
  • 56
  • 12
  • If you're going to answer a question like this one, you should correct the oddities, like the pointless `try..except` and the `in` with a single possibility. – TigerhawkT3 Mar 29 '17 at 09:09
  • Yeah I know, but my main focus was only to correct him with the position of loop. What I did was only change in position of loop which I also stated in my answer that position is wrong. – Ni3_k Mar 29 '17 at 09:11
  • A "try this" code dump with low-quality code is a disservice to the OP and not useful for future visitors. – TigerhawkT3 Mar 29 '17 at 09:13
0

By the your code that's the way it is should work. Look, this part of the code respond of the first question ("Do you love me?"):

#
# First part of your code
#
print("Hello")
print("I love you")
answer = input("Do you love me?")
if answer in ["yes"]:
  print("Yey! I knew it! Thank You!")
if answer in ["no"]:
  print("O-ok, I understand, no worries.")

after your answer you "jump" into the second part of the code ("loop asking again"):

#
# Second part of your code
#
while True:
  try:
    print("Please answer me.")
    answer = input("Do you love me?")
  except ValueError:
    continue
  if answer in ["yes", "no"]:
    break

You need to delete/remove the second part of the your code. Part with loop/cycle:

while True:
  try:
    print("Please answer me.")
    answer = input("Do you love me?")
  except ValueError:
    continue
  if answer in ["yes", "no"]:
    break

Or you can use a loop a good example is in the answer from Ni3_k.

Alisher Gafurov
  • 449
  • 5
  • 15
  • If you're going to answer a question like this one, you should correct the oddities, like the pointless `try..except` and the `in` with a single possibility. – TigerhawkT3 Mar 29 '17 at 09:09