1

So the while loop is supposed to make the user enter one of the two available choices (y/n) but if i input anything, it is shown as incorrect.

I have tried changing the ! to a = and some other minute things but that has done nothing.

print("Hello there " + str(name) + ", are you ready for your 
adventure? Y/N")
    adventure = input()
    while adventure.lower() != "y" or "n":
        print(str(name) + " ,that's not a choice. I'll ask again, are 
you ready for your adventure? Y/N")
        adventure = input()
    if adventure.lower() == "n":
        print("Cowards take the way out quickly.")
        breakpoint
    else:
        print("Come, you will make a fine explorer for the empire!")

It isn't a syntax error but it is a logic error.

blackbrandt
  • 2,010
  • 1
  • 15
  • 32
  • 2
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Carcigenicate Jul 08 '19 at 17:57
  • I realized I should have asked for clarification first before voting. You mean it refuses to stop looping regardless of what input you enter? – Carcigenicate Jul 08 '19 at 17:58
  • @Carcigenicate , yes no matter what variable i put in, it just displays the same code:, that's not a choice. I'll ask again, are you ready for your adventure? Y/N") –  Jul 08 '19 at 18:02
  • See the post I linked to. You can't compare `adventure` against multiple strings by putting `or "n":` at the end. You need to write it out fully `adventure.lower() != "y" and adventure.lower() != "n"`, or use a more advanced method like using a set. You also need to use `and`, not `or`. – Carcigenicate Jul 08 '19 at 18:04
  • @Carcigenicate , i didn't know about that. I have done this technique before a few times. BTW, after adding what you said, the code still isn't working. –  Jul 08 '19 at 18:05
  • If you used it before, it only seemed to work; it didn't actually do what you wanted. `... or "n"` will always be true, no matter what `...` is. That's why it's important to test your code thoroughly. – Carcigenicate Jul 08 '19 at 18:06
  • @Carcigenicate would you mind writing it out for me cos now im a bit confused –  Jul 08 '19 at 18:09
  • Note the code I included in my second comment. The linked post also goes into great detail. – Carcigenicate Jul 08 '19 at 18:10

1 Answers1

1

Change your if statement to:

print("Hello there " + str(name) + ", are you ready for your 
adventure? Y/N")
adventure = input()
while adventure.lower() not in ("y", "n"): # <<<<<---- This line changed

    print(str(name) + " ,that's not a choice. I'll ask again, are 
you ready for your adventure? Y/N")
    adventure = input()
    if adventure.lower() == "n":
        print("Cowards take the way out quickly.")
        breakpoint
    else:
        print("Come, you will make a fine explorer for the empire!")

This is due to how comparisons are done in Python3. See here

Some other fixes you can make to your code:


adventure = input("Hello there {}, are you ready for your adventure? Y/N".format(name)) #Added prompt to input, using string formatting. 

while adventure.lower() not in ("y", "n"): # <<<<<---- Check input against tuple, instead of using `or` statement

    adventure = input(" {}, that's not a choice. I'll ask again, are you ready for your adventure? Y/N".format(name)) #Same as first line
    if adventure.lower() == "n":
        print("Cowards take the way out quickly.")
        break
    else:
        print("Come, you will make a fine explorer for the empire!")

See usage of python input command, string formatting

blackbrandt
  • 2,010
  • 1
  • 15
  • 32