Verifying user input returns wrong answer even when it is correct

This is a snippet of a larger script that I am working on but I think it will be sufficient
enough to get what I’m trying to do accomplished:

P = [“Nova Scotia”, “Ontario”, “Saskatchewan”] # short list of provinces
CC = [“Halifax”, “Toronto”, “Regina”] # their capital cities

for x in range(len(P)):
question = input(str(“what is the Capital of {}?: “.format(P, x+1)))
print(”\n\n”)

if input == (str(CC[x])):
    print("yes")
else:
    print("no")
    continue

If you answer correctly, it says “no”
If you answer incorrectly, it says “no”

I don’t understand why.

Any help or guidance is greatly appreciated.
Thank you in advance.

cogiz
python 3.6.9

If In Doubt, Print It Out.

Immediately before the if statement, what is input and what is (str(CC[x]))? Print them both out and see if they look the same.

I have it figured out now.

P = [“Nova Scotia”, “Ontario”, “Saskatchewan”] # short list of provinces
CC = [“Halifax”, “Toronto”, “Regina”] # their capital cities

for x in range(len(P)):
guess = str(input(“what is the Capital of {}?: “.format(P, x+1)))
print(”\n\n”)

print(input)

print(CC[0])

if guess in (CC[x]):
    print("yes")
else:
    print("no")
    continue

this works perfectly.
SOLVED

Great that you have figured it out. Please next time enclose your code between lines with triple backticks otherwise it shows up incorrectly as you have probably noticed.


This code is not what you probably want:

if guess in (CC[x]):
    print("yes")

For example “fax” is being accepted when only “Halifax” should have been accepted:

>>> "fax" in "Halifax"
True

For strings the operator in checks if the left operand is a substring of the right operand. You should use a different operator to check for equality.