Python Forum
Unexpected output, TypeError and traceback error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unexpected output, TypeError and traceback error (/thread-9939.html)



Unexpected output, TypeError and traceback error - fier259 - May-05-2018

This is my first python program and forum post so bear with me if I don't this right. I have an if statement asking for instructions or to begin my game. This program will be presented to middle schoolers so I made a while loop for the name input statement that won't let them continue if they input "69", "420", or "ur mom". But for some reason, it doesn't ask the input statement asking the user for a name and gives a typeError. The exact error message is
 Traceback (most recent call last):
  File "C:/Users/break/PycharmProjects/Tutorial/GameExp.py", line 16, in <module>
    while name in nameTwo:
TypeError: 'in <string>' requires string as left operand, not set [python]

This is my code: 
[python] # This section gives the instructions or lets the user start the game

instruction = input("Press [H] for instructions or any other key to start:")
if instruction == "H":
 print("This game is a turn based adventure in which you have to gather information, make tactical choices, and fight enemies to win. Enter all inputs with capital letters.")

else:
 print("Beginning game...")

# A basic 14 year old proof name input
name = {"420", "ur mom", "69"}
nameTwo = " "
while name in nameTwo:
  name = input("Please enter a name:")
  if name == "420":
   print ("Try again...")
  elif name == "ur mom":
      print("Try again...")
  elif name == ("69"):
      print("Try again...")
      break
else:
      print("Hello"+ name)
Thanks Python community for taking the time to consider this.


RE: Unexpected output, TypeError and traceback error - Gribouillis - May-06-2018

You have an error because when the name in nameTwo part is evaluated the first time, you just defined name as a set and nameTwo as a string. You can use the following loop instead
invalid = {'', '420', 'ur mom', '69'}
while True:
    name = input('Please enter a name: ').strip()
    if name.lower() in invalid:
        print('Try again...')
    else:
        break
print('Hello', name)



RE: Unexpected output, TypeError and traceback error - fier259 - May-06-2018

Thank you, that really helped