# Verifying user input returns wrong answer even when it is correct

**URL:** <https://discuss.python.org/t/verifying-user-input-returns-wrong-answer-even-when-it-is-correct/19956>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 13, 2022, 5:35pm UTC](https://discuss.python.org/t/verifying-user-input-returns-wrong-answer-even-when-it-is-correct/19956 "2022-10-13T17:35:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cogiz](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@cogiz](https://discuss.python.org/u/cogiz)\
**Post date:** [October 13, 2022, 5:35pm UTC](https://discuss.python.org/t/verifying-user-input-returns-wrong-answer-even-when-it-is-correct/19956/1 "2022-10-13T17:35:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 13, 2022, 5:37pm UTC](https://discuss.python.org/t/verifying-user-input-returns-wrong-answer-even-when-it-is-correct/19956/2 "2022-10-13T17:37:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cogiz](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@cogiz](https://discuss.python.org/u/cogiz)\
**Post date:** [October 13, 2022, 6:16pm UTC](https://discuss.python.org/t/verifying-user-input-returns-wrong-answer-even-when-it-is-correct/19956/3 "2022-10-13T18:16:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [October 13, 2022, 9:32pm UTC](https://discuss.python.org/t/verifying-user-input-returns-wrong-answer-even-when-it-is-correct/19956/4 "2022-10-13T21:32:25Z")

</div>

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:

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

```

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

```python
>>> "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.
