NameError:未定义设备

时间:2017-05-10 17:06:45

标签: 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.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

1 个答案:

答案 0 :(得分:0)

Zwol的评论是正确答案:

  

device是choose_device中的局部变量。使其可访问   从solution_for_phones里面你必须把它传递给那里   参数。

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

您使用solution_for_phones定义def solution_for_phones():。但它需要device的值,因为它使用device。因此,首先将您的函数定义更改为:

def solution_for_phones(device):

现在solution_for_phones需要将device的值传递给它才能运行。

接下来,您需要确保每次拨打solution_for_phones时都传递device的值。在您choose_device() solution_for_phones()的任何地方,您需要将其替换为solution_for_phones(device)

你也应该谷歌类似&#34; python在函数之间传递值&#34;并阅读更多内容,例如&#34; position&#34; vs&#34;关键字&#34;功能参数。