opening and implementing a txt file into a Dictionary [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 8 years ago.
Improve this question
I am having problems with the following lines of code:
Dictionary = {}
with open("Text Docs/clues.txt", "r") as l:
for l in clues:
Dictionary[l[0]] = Dictionary[l[1]]
getting an error that says:
(file location), line #, in function
for l in clues:
NameError: global name 'clues' is not defined

You are not iterating over file object, you probably meant:
Dictionary = {}
with open("Text Docs/clues.txt", "r") as l:
for line in l:
...

There is no variable called clues defined. The file object you created is called as l. So your for loop should iterate over that.
Use:
for line in l:
#do something

Related

UnboundLocalError local variable is undefined [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 2 years ago.
Improve this question
non_punctuation_texts=""
for char in file_contents:
if char not in punctuations:
non_punctuation_text=non_punctuation_text+char
words=non_punctuation_text.split()
clean_words=[]
frequencies={}
for word in words:
if word.isalpha():
if word not in uninteresting_words:
clean_words.append(word)
for alpha_word in clean_words:
if alpha_word not in frequencies:
frequencies[alpha_word]=1
else:
frequencies[alpha_word]+=1
im getting the error that my variable is not defined, may someone point out my error and help me wiht a solution?
EXACT ISSUE: UnboundLocalError: local variable 'non_punctuation_text' referenced before assignment
As already pointed out by #MattDMo, on line 1 you have
non_punctuation_texts=""
Then on line 4 you have
non_punctuation_text=non_punctuation_text+char
On line 1 you spell the variable with an s, and on line 4 you do not have an s. To fix your problem, you need to use the same spelling to reference the same variable.

"UnboundLocalError: local variable 'input' referenced before assignment" for nearly every input in my code [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 2 years ago.
Improve this question
x = int(0)
ans1 = int(0)
ans = int(0)
with open('risk_q.txt') as r:
for x in range(12):
mylist = [line.rstrip('\n') for line in r]
ans1 = int(input()) #Error occurs in this line
This error occurred with a different input earlier in the code so I got the input before the function and then passed it through the parameter of the function.
Check back through your code. It appears as though you have used input as a variable name, and therefore overwritten the builtin function.

.remove function not working with an 'if' check in lists [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'm fairly new to programming and python itself- I'm trying to use the .remove function to delete an item from a list if it exists in that list (as to not get the nameError).
q = ["cat","dog","fish","hamster","horse"]
#Request element name to delete from queue
removeElement = input("Please type in the element name to remove from the queue: ")
#Remove the given element from the list
q.remove(removeElement) if 'removeElement' in q else None
print(q)
Unfortunately, if I try and use the 'if' checker it the item isn't removed from my list- why is this and how can I fix this issue?
You have to use the variable's name not a string:
q.remove(removeElement) if removeElement in q else None

How to resolve "return" outside function 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 4 years ago.
Improve this question
phone_letters =[" ", "1", "ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ","*","0","#" ]
key = 0
string = input("Enter a Letter: ",)
while key < 10 :
if string in phone_letters[key]:
print(key)
return key
else:
key = key+1
return "not found"
I am getting error 'return' outside function; I checked indentation and still the error continues.
return may only occur syntactically nested in a function definition
Source: https://docs.python.org/3/reference/simple_stmts.html#the-return-statement
Your return is not nested inside a function. Therefore, the error.
You can use print("not found") if you want to display the text.

error in writing a text file in python [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 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:

Categories