0
import keyword
keywords = (keyword.kwlist)

user_String = input("enter a string: ")

answer = (user_String == keywords)

print ("it is " + str(answer) + " that the string '" + user_String + "' is a 
keyword.")

I'm trying to write a program that asks the user for a string and tests whether it is a keyword. But the trouble I'm having is even if I enter a valid keyword it still prints False

Angel Valenzuela
  • 377
  • 1
  • 5
  • 15

2 Answers2

1

The problem is that you are comparing a string with a collection (keywords).

Instead, try using something like this:

if user_String in keywords:
    print("That is a keyword")
else: 
    print("That is not a keyword")
Ryan_DS
  • 750
  • 7
  • 28
  • 1
    Python needs proper indentation for blocks, and semicolons are not necessary (they are *allowed*, but you should not ever need to use them unless your doing code golf and squeezing everything into one line). – Blckknght Jun 10 '18 at 08:26
  • @Blckknght thanks for that. I'll stick to languages I fully understand to prevent misleading beginners. – Ryan_DS Jun 10 '18 at 08:30
0

You should use "contains" function instead of checking equality. Moreover, you have a function called isKeyWord(string) that checks this exact thing.

Or251
  • 196
  • 10