Why my syntax is error? (TypeError: 'tuple' object is not callable) - tuples

My syntax is like this
print ("Hello!")
print ("Ayah berkata, \"Semangat\"")
print ("Ayah berkata,", end=" ")
print ("\"Semangat\"")
and my output is like this
TypeError Traceback (most recent call last)
<ipython-input-8-122075a697db> in <module>()
----> 1 print ("Hello!")
2 print ("Ayah berkata, \"Semangat\"")
3 print ("Ayah berkata,", end=" ")
4 print ("\"Semangat\"")
TypeError: 'tuple' object is not callable
I'm code at google colab

You have created a variable named "print" which is a tuple. You have to remove or rename the variable "print".
>>> print = ("test", "test") # <- somewhere there might be a linge likethis one
>>> print ("Anything")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable

Related

How to fix "EOF when reading a line" using plugin Python-Notepad++

I'm using python plugin in Notepad++ and trying to use input() in a script I get this error
>>> name=input()
Traceback (most recent call last):
File "<console>", line 1, in <module>
EOFError: EOF when reading a line
I've tried solutions in other threads of SO like using try/except block as below, but still getting the error. Is there a workaround for this? Thank you.
>>> try:
... input("Please enter something")
... except:
... break
File "<console>", line 4
SyntaxError: 'break' outside loop
>>> try:
... name=input()
... except EOFError:
... break
File "<console>", line 4
SyntaxError: 'break' outside loop
Traceback (most recent call last):
UPDATE
>>> try:
... input("Please enter something: ")
... except EOFError as e:
... print(e)
...
Please enter something: EOF when reading a line
>>> abc
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'abc' is not defined
change break to pass and you should pass that error or use
except EOFError as e:
print(e)
so you will know when it happened. All I can help as I can't replicate it here, works just fine for me. Use break only in loops.

Why isdigit is working but isdecimal is not for a simple age code

This is the part of code I am talking about:
while True:
print 'What is your age(only numeric):'
age = raw_input()
if age.isdigit():
break
print 'Please try again. We take only numeric value.'
When I replace isdigit with isdecimal, I get this error no matter what value i enter.
Traceback (most recent call last):
File "test.py", line 4, in <module>
if age.isdecimal():
AttributeError: 'str' object has no attribute 'isdecimal'
Although it runs fine with isdigit.
Also, when I replace raw_input with input, it give this error.
Traceback (most recent call last):
File "test.py", line 4, in <module>
if age.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'
Am I doing something wrong which I am not able to catch? I am new to python and did my research about isdigit, is decimal, input and raw_input before putting down question here. It may be very minute error but I am not sure what is it.
For the first part:
In Python 2, isdecimal is a method of the unicode class, not of str (i.e. bytes). Hence:
>>> '3.2'.isdecimal()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'isdecimal'
>>> u'3.2'.isdecimal()
For the second part, input tries to guess what kind of input it gets, and will properly cast "1" into an int, while raw_input will always return the raw string. Since isdigit is a method defined in the str class, you get the error.

Is there a reason `tolist()` can't operate on a list?

I often think it would be nice to have the following work even if the original object is a list... Is there a reason why this functionality is not implemented (in python 2.7)?
Example:
>>> a = [1,2,3,4]
>>> a.tolist()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'tolist'

Slice operator in python

>>> word="python"
>>> word[42]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[4:42]
'on'
>>> word[42:]
''
My question is that why slice operator doesn't give IndexError?

python3 'Nontype object' is not subscriptable

How can I solve this problem?
def median(lst):
lst_s=lst.sort()
k=len(lst)
if k%2==0:
return ((lst_s[k/2]+lst_s[k/2-1])/2)
elif k%2==1:
return (lst_s[k/2-1/2])
res = median([1,2,3,7,6,5])
print('median:',res)
when I run this, it says
Traceback (most recent call last):
File "E:\lab5_p4_2015123176.py", line 13, in <module>
res = median([1,2,3,7,6,5])
File "E:\lab5_p4_2015123176.py", line 7, in median
return ((lst_s[k/2]+lst_s[k/2-1])/2)
TypeError: 'NoneType' object is not subscriptable
The problem is that list.sort() sorts the items of the list in place thus returns None. In your median function you need to replace lst_s = lst.sort() by lst_s = sorted(lst) if you don't want to modify element order in the original list.