-2

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
allo
  • 3,955
  • 8
  • 40
  • 71
  • https://stackoverflow.com/help/mcve – handle May 10 '17 at 17:08
  • 1
    The error is pretty straightforward: the variable `device` is not defined and you're trying to use it in the line: `ReportFile.write ("\n"+"Case No is : "+(device))` – Nir Alfasi May 10 '17 at 17:09
  • 2
    `device` is a _local_ variable within `choose_device`. To make it accessible from inside `solution_for_phones` you will have to pass it there as an argument. – zwol May 10 '17 at 17:10
  • and how do i do that? can you show me pls. much appriciated – Maruf Talukdar May 10 '17 at 17:11
  • Suppose you defined `def solution_for_phones(device):` and called it with `solution_for_phones(device)`. Now the function takes a parameter telling it what device to use and the caller uses its local variable to set the parameter. – tdelaney May 10 '17 at 17:28
  • Possible duplicate of [python NameError: name 'xxx' is not defined](http://stackoverflow.com/questions/35920320/python-nameerror-name-xxx-is-not-defined) – Arya McCarthy May 10 '17 at 17:33
  • I tried to improve your question a bit, but when i removed all the unneccessary parts (we know, that it may be a stupid question, etc.) SO told me the question contains almost only code, so i could not save it. So PLEASE reformat it. Describe the actual problem, don't add "old and boring" to the title and use the shortest code possible to get to the error. – allo May 10 '17 at 18:42
  • I disagree with the comments that this question needs to be improved. Clearly multiple people here were able to correctly identify the problem and required solution from the code posted. It's not the perfect minimal SO question but is much clearer than most questions I see from new users. – Max Power May 10 '17 at 20:18

1 Answers1

0

Zwol's comment is the correct answer:

device is a local variable within choose_device. To make it accessible from inside solution_for_phones you will have to pass it there as an argument.

Here is how that works in code, since from your last comment it seems like you're still confused.

You define solution_for_phones with def solution_for_phones():. But it needs a value for device to work, since it uses device. So first change your function definition to:

def solution_for_phones(device):

Now solution_for_phones requires a value for device to be passed to it to run.

Next you need to make sure that everytime you call solution_for_phones, you pass a value for device. Everywhere in choose_device() where you have solution_for_phones(), you need to replace that with solution_for_phones(device).

You should also probably google something like "python passing values between functions" and read up some more on this, such as the difference between "positional" vs "keyword" parameters of functions.

Max Power
  • 8,265
  • 13
  • 50
  • 91