I’m working on a simple login system for my Python project and running into a problem with string comparison. Here’s the code that’s causing trouble:
user_input = input()
if user_input('Johnson'):
print("Access granted! Hello Agent Johnson")
When I try to run this, I keep getting this error message:
if user_input('Johnson'):
TypeError: 'str' object is not callable
I’m pretty new to Python and can’t figure out what’s wrong with my syntax. The program crashes every time I try to test it. Can someone help me understand what I’m doing incorrectly here?
The error occurs because you’re treating user_input as a function when it’s actually just a string variable. The correct way to compare the input is using the == operator, like this: if user_input == 'Johnson':. It’s an easy mistake to make, especially if you’re coming from a different programming background. Additionally, remember that string comparisons are case-sensitive and can be affected by leading or trailing spaces, so consider normalizing the input with .strip() or .lower() in a complete solution.
ohh gotcha! ur treating user_input like a func but it’s just a str. just change that line to if user_input == 'Johnson': instead. use == for string comparison, otherwise error!
Yeah Mike’s right, but here’s some context since you’re new to Python.
Those parentheses after user_input make Python think you’re calling a function. But user_input is just a string variable with whatever the user typed.
I’ve made this mistake tons of times switching between languages. Spent an hour debugging something similar in my early days because I was thinking in JavaScript.
Heads up - this only works if they type exactly “Johnson”. “johnson” or “JOHNSON” won’t match. You might want .lower() or .strip() to handle different cases and extra spaces.