i know there are a lot of people asking a similar question and i also understand what the answers are trying to say but no matter what i try,it just doesnt work. someone help! here is my code (well a shorter version)
import random
##opening all the files and reading them line by line.
file_1 = open("devices.txt","r")
read_1 = file_1.readlines()
file_2 = open("phones.txt","r")
read_2 = file_2.readlines()
def choose_device():##creating function
device = input(read_1[0]).lower()##asking the user by printing a question from file_1
if device == "phone" or device == "phones" or device == "smartphone" or device == "smartphones" or device == "1":
brand = input(read_2[0])
if brand == "samsung":
version = input(read_2[1])
raw_memory = input(read_2[4])
solution_for_phones()
elif brand == "iphone":
version = input(read_2[2])
raw_memory = input(read_2[4])
solution_for_phones()
elif brand == "sony":
version = input(read_2[3])
raw_memory = input(read_2[4])
solution_for_phones()
else:
print(read_2[5])
do_again()##restart
def solution_for_phones():
datadict = {} ##creating a dictionary
with open('phonesolution.txt') as file: ##opening file
for rec in file: ##looping through every line
rec = rec.split(':') ##delimiter is :
problem = rec[0] ##first values before the delimiter are saved as problems
answer = rec[1] ##second values after the delimiter are saved as answers
problem = problem.split(" ") ##problems is further split by the delimiter "space"
for item in problem: ##every word in the problem section is assigned to an answer
datadict[item] = answer
user_problem = input('What is the problem?: ')##asking the user where the problem is
split_answer = user_problem.split(" ")##splitting the users answer into separate words
for option in datadict.keys():
if option in split_answer:##mathcing the users answer to keywords in the problem
print(datadict[option])
else:
CaseNo = (random.randrange(100000,999999))
print (CaseNo)
ReportFile = open('not_found.txt', 'a')
ReportFile.write ("\n"+"-------------------------------------------")
ReportFile.write ("\n"+"Case No is : "+str(CaseNo))
ReportFile.write ("\n"+"Case No is : "+(device))
ReportFile.close
do_again()
and here is the error message. anybody knows how to fix it?
welcome to our trouble shooting system
what device do you have a problem with? (these are the options:1.smartphones, 2.laptops, 3.game consoles)
smartphones
what brand is your phone? (eg: samsung, iphone)
samsung
what version is your samsung? (eg:J3, S7 edge)
J3
what is the memory size? (eg:8g, 16gb)
8
What is the problem?: vZrfvSZ
109451
Traceback (most recent call last):
File "C:\Users\hp\Downloads\A453\task 3\mine\task 3 just for practice.py", line 156, in <module>
choose_device()##calling the function
File "C:\Users\hp\Downloads\A453\task 3\mine\task 3 just for practice.py", line 110, in choose_device
solution_for_phones()
File "C:\Users\hp\Downloads\A453\task 3\mine\task 3 just for practice.py", line 58, in solution_for_phones
ReportFile.write ("\n"+"Case No is : "+(device))
NameError: name 'device' is not defined