'builtin_function_or_method' object is not subscriptable." 5 [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Trying to make a code about usernames:
User=input("type a username with 4 numbers, then 2 letters.")
test=(User.isdigit[0:3])
trial=(User.isaplha[4:5])
if test ==True:
if trial ==True:
print("This is a valid username.")
else:
print("The last two characters must be numbers.")
else:
print("The first four characters must be letters.")
I receive this error
Traceback (most recent call last):
File "python", line 2, in
TypeError: 'builtin_function_or_method' object is not subscriptable

isdigit() and isalpha() don’t support indexing parameters and if you want to apply these builtin functions on specific substring try indexing your string like
test = User[0:3].isdigit()
It will work perfectly without any errors 👍🏻

Related

I keep getting errors with an "if var.startswith" statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm trying to make it so that I can input someone into console and have it set a variable to it, and every time with the if statement it gives the error
File "main.py", line 58, in <module>
if meInput.startswith("%send"):
AttributeError: 'builtin_function_or_method' object has no attribute 'startswith'
Here's the code:
if input.startswith("%send"):
myinput = input.split(" ", 2)[2]
channel = client.get_channel(12324234183172)
I've tried putting it into a variable such as variable = input then changing the if statement to match the variable, but it does the same thing.
Read the error message carefully! It is telling you that input is not a string, but a function — a function that would return a string if you called it, but you didn’t. Try this instead:
if input().startswith("%send"):
Note the parentheses. That is how you call a function in Python, and in most other languages.

'TypeError: can only concatenate tuple (not "str") to tuple' error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i am new to python and i'm trying to learn i made this small piece of code
weight = input('How much do you weigh in pounds? ')
weight_kg = int(weight)* 0,453592
print (weight_kg + 'is your weight is kilogrammes')
but when i run it i get this error
Traceback (most recent call last):
File "C:/Users/hh/Desktop/PYTHON-LEARNING!/app.py", line 3, in <module>
print (weight_kg + 'hi')
TypeError: can only concatenate tuple (not "str") to tuple
There are two problems here, one is that you multiply by a float with a comma as the decimal separator, and the second is that you can't concatenate a float and a string in a print statement. Below is the fixed code - I use an f-string to print the final output, which requires Python 3.6.
weight = input('How much do you weigh in pounds? ')
weight_kg = int(weight)* 0.453592
print (f"{weight_kg} is your weight is kilogrammes")

How to fix the len() command returning TypeError? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to work out the length of a string in python however I can't seem to get a correct response. If someone could tell me what I am doing wrong, I would be very grateful.
Code:
name = input("Type a string!")
name = name.upper
lenname = len(name)
if lenname == 3:
print("Success")
else:
print("Fail")
I expect the output to be Success when I enter abc however I receive:
TypeError Traceback (most recent
call last)
<ipython-input-20-1de45569cf05> in <module>
1 name = input("Type a string!")
2 name = name.upper
----> 3 lenname = len(name)
4 if lenname == 3:
5 print("Sucess")
TypeError: object of type 'builtin_function_or_method' has no len()
You're trying to access upper like it's a field, not a method. On the line above where you use len, do name = name.upper().

How to declare and fill an empty array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got an error using numpy.zeros, it seems like my value a can't be filled since i got an error:
track=2
a=np.zeros(shape=(3,2))
eps_real=a(Cp-0.5,2)/2*3.14*track
eps_imag=a(Cp-0.5,2*track)/2*3.14*track
tau=a(Cp-1,2)
print tau
My error when i ran is:
Traceback (most recent call last):
File "Main.py", line 35, in <module>
eps_real=a(Cp-0.5,2)/2*3.14*track
TypeError: 'numpy.ndarray' object is not callable
Collection members in Python use square brackets ([]), not parentheses. So your code should be:
eps_real=a[Cp-0.5,2]/2*3.14*track
eps_imag=a[Cp-0.5,2*track]/2*3.14*track
tau=a[Cp-1,2]
Parentheses are used for calling functions, hence the error message object is not callable

Checking chars from string using user unput [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
When I declare the variable for the string in the code it works, for example:
messagetoencode="readthis"
encodedMsg=[]
for letter in messagetoencode:
encodedMsg.append(encode[letter.upper()])
print "result is ", encodedMsg
However when I try and do the same operation with the user input it doesn't work:
lst = list(raw_input("Please enter a message to encode: "))
encodedMsg=[]
for letter in lst:
encodedMsg.append(encode[letter.upper()])
print "result is ", encodedMsg
and I get a traceback error, any ideas why?
Error:
Please enter a message to encode: hello
Traceback (most recent call last):
File "Untitled 3.py", line 27, in <module>
obfuse.append(encode[letter.upper()])
KeyError: 'O'
The problem is with the key O in the dictionary. It doesn't exist on the text, which is why no error happened. But if you include o in your input, you'll get this error.
Well, the real solution is to have the key'O' in your encoding dictionary.
To deal with unexpected input, you can do a check like this:
for letter in lst:
if letter not in encode:
raise KeyError("Sorry i don't know how to encode this letter!")
encodedMsg.append(encode[letter.upper()])
The problem is, O is not in the encode dictionary. If you want to assign a default value, instead of failing like this, you can use dict.get method like this
obfuse.append(encode.get(letter.upper(), None))
Now, this will return None if any of the keys are not found in encode. You might actually want to include a mapping for O in your code.

Categories