0

I am making a python game of sorts and I am coming across an error. This error is saving previously user inputted data into a list even though I clear the list to the default hints at the start of the run.

The code looks like this:

pairLet = ["A#", "M*", "N%"]

letTer=input("Write a letter here (in capital letters) ")

symBol=input("Write a symbol here ")


if letTer or symBol in pairLet:
    print("That letter/symbol is already in use")
    print(pairLet)

else:
pairLet.append((letTer) + (symBol))
    print(pairLet)

When I restart the code to enter the same letter and symbol pairing it shows this code:

Write a letter here (in capital letters) B
Write a symbol here ^
That letter/symbol is already in use
["A#", "M*", "N%"]

It prints the contents of the variable "pairLet" and the pairing inputted clearly aren't in there. How am I able to fix this?

Thanks -

Jake

Torxed
  • 22,866
  • 14
  • 82
  • 131
Jake
  • 1
  • 1
  • 1
    Your `if` should be: `if letTer in pairLet or symBol in pairLet:` – Alex Nov 14 '18 at 14:59
  • 1
    It should actually be `if letTer+symBol in pairLet`. That capitalization in the middle of the words also makes it really hard to read. – Patrick Haugh Nov 14 '18 at 15:00
  • @Alex The real answer is what Patrick suggest. What you are doing is checking if the letter is in the list or the symbol, not the combination of both at the same time. – MooingRawr Nov 14 '18 at 15:00
  • I see, that wasn't initially clear. – Alex Nov 14 '18 at 15:03

1 Answers1

-1
if letTer or symBol in pairLet:
    print("That letter/symbol is already in use")
    print(pairLet)

you'll want

if letTer in pairlet or symBol in pairLet:
    print("That letter/symbol is already in use")
    print(pairLet)

Because letTer is evaluated to True (if letTer isn't an empty str, it's evaluated as True)

IanQ
  • 1,831
  • 5
  • 20
  • 29