Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NameError:未定义设备_Python - Fatal编程技术网

Python NameError:未定义设备

Python NameError:未定义设备,python,Python,我知道有很多人问了一个类似的问题,我也明白答案是什么,但不管我怎么做,都不管用。 谁来帮忙! 这是我的代码以及一个较短的版本 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.readlin

我知道有很多人问了一个类似的问题,我也明白答案是什么,但不管我怎么做,都不管用。 谁来帮忙! 这是我的代码以及一个较短的版本

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()
下面是错误消息。有人知道怎么修吗

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

Zwol的评论是正确的答案:

device是choose_设备中的局部变量。让它变得容易接近 从手机的内部解决方案中,您必须将其作为 争论

下面是代码中的工作原理,因为从您上次的评论来看,您似乎仍然感到困惑

您可以使用def solution_for_phones:为_电话定义解决方案。但它需要一个设备工作的值,因为它使用设备。因此,首先将函数定义更改为:

电话设备的def解决方案:

现在,手机的解决方案需要一个值,以便将设备传递给它运行

接下来,您需要确保每次为手机调用解决方案时,都会为设备传递一个值。在choose_device中的任何地方,如果您有针对手机的解决方案,则需要将其替换为针对手机的解决方案设备


您可能还应该在google上搜索类似python的东西,在函数之间传递值,并阅读更多关于这方面的内容,例如函数的位置参数和关键字参数之间的差异。

错误非常简单:变量设备未定义,您试图在以下行中使用它:ReportFile.write\n+Case No is:+device设备设备设备是choose\u设备中的局部变量。要使其从内部解决方案手机访问,您必须将其作为参数传递到那里。我该怎么做?你能给我看看吗。非常感谢您为电话设备定义了def solution_,并将其与电话设备的solution_一起调用。现在,函数接受一个参数,告诉它使用什么设备,调用方使用其局部变量来设置参数。