添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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)
what is the memory size? (eg:8g, 16gb)
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
                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, 2017 at 17:09
                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, 2017 at 17:10
                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, 2017 at 17:28
  

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.