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
>>> a=int(input())
2
>>> b=int(input())
4
>>> c=input()
r
>>> if c==r:
print(b+1)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
if c==r:
NameError: name 'r' is not defined
The function input() returns a string, so if you want to check if c is the string r, you have to add double quotes, or single quotes. Both are used to represent strings in Python:
if c == 'r':
or
if c == "r":
You have to check with "r" not r. As follows:
>>>if c=='r':
>>> print(b+1)
5
This is because when you input "r" it is as a string. When you did the c==r you were comparing to the variable r. This is clearly not what you wanted. Therefore you use c=='r' to compare strings.
Related
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 2 years ago.
Improve this question
This code:
a = 10
b = 20
print(f "The variable a is {a} and the variable b is {b}.")
returns this error:
File "main.py", line 3
print (f "The variable a is {a} and the variable b is {b}")
^
SyntaxError: invalid syntax
The version I'm using is after 3.6, so it should work. I'm using Anaconda's prompt, if that's a problem.
you have a space between f and the string. Remove it and everything will work.
The syntax error points to the end of the string. If it pointed to the beginning of the string it would give you a better hint at the problem: after the f, which is interpreted as a variable in this case, a string is unexpected.
You need this:
a = 1
b = 2
test_str = f"{'The variable a is'} {a} {'and the variable b is '}{b}"
print(test_str)
Between the curly braces, string interpolation takes place. The variables for a and b are within two of the curly brace sets and hard-coded strings are within the other two.
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 have declared a list and trying to print it. Here is my code:
list1 = ["hello", 23, 5.0, ["hi", 15,[2,3,4,'5',[3,7,8]]], "xyz"]
print(list1[3][2][2][2])
And i got the following errors:
Traceback (most recent call last):
File "C:/Users/ASUS/Desktop/Data/tr.py", line 2, in <module>
print(list1[3][2][2][2])
TypeError: 'int' object is not subscriptable
Process finished with exit code 1
You are pointing to the wrong index. It's not an error about the list declaration. It's an error about your printing. Please read the errors carefully before asking.
Here is what your print statement points to:
list1[3] -> ["hi", 15,[2,3,4,'5',[3,7,8]]]
list1[3][2] -> [2,3,4,'5',[3,7,8]]
list1[3][2][2] -> 4
So when you try to print list1[3][2][2][2] it tries to access 2nd index of int 4. Which is not subscriptable.
Open a REPL and see for yourself!
>>> list1 = ["hello", 23, 5.0, ["hi", 15,[2,3,4,'5',[3,7,8]]], "xyz"]
>>> list1[3][2][2]
4
>>> _
This is why list1[3][2][2][2] ends with an error: list1[3][2][2] is not a list.
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 ๐๐ป
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 6 years ago.
Improve this question
def open_file(filename):
file_open= open(filename,"r")
return file_open
When I try and call the function I get the following results:
>>> open_file(random.txt)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
open_file(random.txt)
NameError: name 'random' is not defined
try
open_file('random.txt')
Strings in Python need to be quoted.
random is being interpreted as an object, and is undefined.
You forgot quotes:
open_file('random.txt')
python thinks random is an object, which obviously you didn't define. The quotes make it a string.
you just need to input the filename as a string; here's how it must be done:
>>> open_file('random.txt')
note that your function works just fine, all you need to do is call it properly.
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 am writing a text file with the following code(pl in the code is a list of lists):
out_file = open("par.txt", 'w')
out_file.write("id\ttrans_id\ttype\tstatus\tname\ttrans_type\ttrans_status\ttrans_name\n")
for lst in pl:
out_file(lst[0].split()[1],"\t",lst[1].split()[1],"\t",lst[2].split()[1],"\t",lst[3].split()[1],"\t",lst[4].split()[1],"\t",lst[5].split()[1],"\t",lst[6].split()[1],"\t",lst[7].split()[1])
out_file.close()
BUT it gives this error:
Traceback (most recent call last):
File "test.py", line 25, in <module>
out_file(lst[0].split()[1],"\t",lst[1].split()[1],"\t",lst[2].split()[1],"\t",lst[3].split()[1],"\t",lst[4].split()[1],"\t",lst[5].split()[1],"\t",lst[6].split()[1],"\t",lst[7].split()[1])
TypeError: 'file' object is not callable
You need to change the loop to something like:
for lst in pl:
out_file.write('\t'.join(x.split()(1) for x in lst))
out_file.write('\n')
Try this:
with open("par.txt", "a+") as f:
f.write("id\ttrans_id\ttype\tstatus\tname\ttrans_type\ttrans_status\ttrans_name\n")
for lst in pl:
f.write(("{}\t"*8).format(lst[0].split()[1],lst[1].split()[1],lst[2].split()[1],lst[3].split()[1],lst[4].split()[1],lst[5].split()[1],lst[6].split()[1],lst[7].split()[1]))
It's not going to overwrite your file.You should change that 8 variable what is the length of your list, which is we dont know that list.
Example Output: