Python Forum
Unexpected output, TypeError and traceback error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected output, TypeError and traceback error
#1
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.
Reply
#2
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)
Reply
#3
Thank you, that really helped
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 444 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  Error with output djprasanna 1 509 Nov-28-2023, 06:40 PM
Last Post: deanhystad
  Unexpected output Starter 2 439 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 654 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 467 Oct-24-2023, 05:56 AM
Last Post: buran
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,181 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,125 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  Python Traceback Error mg24 2 903 Nov-21-2022, 02:35 PM
Last Post: snippsat
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,856 Oct-17-2022, 06:29 PM
Last Post: paulo79
  "<class 'typeerror'>: don't know how to convert" error GiggsB 3 3,236 Feb-28-2022, 06:45 PM
Last Post: GiggsB

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020