它显示我的一个变量没有定义,而从我所看到的情况来看,它是

2024-06-02 09:04:44 发布

您现在位置:Python中文网/ 问答频道 /正文

这是出现的错误:

What type of device is it?phone
What make of phone is it? [Iphone, Samsung or other]samsung
Traceback (most recent call last):
  File "C:\Users\Chris\Documents\Chris\School\School\computing\task 3 testing.py", line 79, in <module>
    if ('4') in model_iphone:
NameError: name 'model_iphone' is not defined

我不知道怎么修。如果有人能指出我的代码中的一些潜在问题,那会很有帮助。我知道这不是最有效的代码,但它将是伟大的,有一些帮助,谢谢。你知道吗

我的代码:

apple_question = []


for row in apple_file:
    apple_question.append(row)


other = open("task 3 other questions.csv")
other_file = csv.reader(other)

other_question = []

for row in other_file:
    other_question.append(row)

pre_questions = open("task 3 pre questions.csv")
pre_questions_file = csv.reader(pre_questions)

pre_questions_question = []

for row in pre_questions_file:
    pre_questions_question.append(row)

device_type = input(pre_questions_question[1][0])
device_type.lower()

if ('phone') in device_type:
    make = input(pre_questions_question[2][0])
    make.lower()

elif ('iphone')in make:
    model_iphone = input(samsung_question[1][0])
    model_iphone.lower()

elif ('samsung') in make:
    model_samsung = input(samsung_question[1][0])
    model_samsung.lower()

elif ('other') in make:
    make_other = input(other_question[0][0])
    make_other.lower()

    model_other = input(other_question[1][0])
    model_other.lower()

    problem_other = input(other_question[2][0])
    problem_other.lower

    info = print(other_question[3][0])

#other 
    text_file = open('Otherdevice.txt', 'w' )

    text_file.write(make_other)

    text_file.write(model_other)

    text_file.write(problem_other)

    text_file.close()

#apple

if ('4') in model_iphone:
    ios = input(apple_question[3][0])

elif ('5') in model_iphone:
    ios = input(apple_question[3][0])

elif ('5c') in model_iphone:
    ios = input(apple_question[3][0])


if ('7') in ios:
    memory = input(apple_question[4][0])

elif ('8') in ios:
    memory = input(apple_question[4][0])

elif ('9') in ios:
    memory = input(apple_question[4][0])

else:
    print("Sorry but you have entered invalid or not surported information please try again")


if ('8gb') in memory:
    query = input(apple_question[5][0])

elif ('16gb') in memory:
    query = input(apple_question[5][0])

elif ('32gb') in memory:
    query = input(apple_question[5][0])

#samsung

if ('s4') in model_samsung:
    android = input(samsung_question[2][0])

elif ('s5') in model_samsung:
    android = input(samsung_question[2][0])

elif ('s6') in model_samsung:
    android = input(samsung_question[2][0])



else:
    print("Sorry but you have entered invalid or not surported information please try again")


if ('jellybean') in android:
    service_provider = input(samsung_question[3][0])

elif ('lollipop') in android:
    service_provider= input(samsung_question[3][0])

elif ('marshmallow') in android:
     service_provider = input(samsung_question[3][0])

Tags: inappleinputmakemodelifprelower
1条回答
网友
1楼 · 发布于 2024-06-02 09:04:44

很难理解代码中发生了什么,但据我所见,似乎永远不会生成变量model_iphone。相反,因为您输入的是“samsung”,所以代码生成了一个名为model_samsung的变量,该变量的作用是相同的。不要让所有这些不同的变量做相同的事情(并且只初始化其中一个),试着只做一个统一的变量:

#Previous code...

#check for device type
if ('phone') in device_type:
    make = input(pre_questions_question[2][0])
    make.lower()

#separate if statement block to process the make and model after determining the type
if ('iphone')in make: #I CHANGED THIS LINE AS WELL TO WHAT YOU INTENDED IT TO DO (I think)
    model = input(samsung_question[1][0])
    model.lower()

elif ('samsung') in make:
    model = input(samsung_question[1][0])
    model.lower()

elif ('other') in make:
    make_other = input(other_question[0][0])
    make_other.lower()

    model = input(other_question[1][0])
    model.lower()

    problem_other = input(other_question[2][0])
    problem_other.lower

    info = print(other_question[3][0])

#other 
    text_file = open('Otherdevice.txt', 'w' )

    text_file.write(make_other)

    text_file.write(model)

    text_file.write(problem_other)

    text_file.close()

#apple

#ask the corresponding questions
if ('4') in model: 
    ios = input(apple_question[3][0])

#Continue code...

请注意,同一问题的所有输入现在都是如何传递到同一个变量的,因此无论调用if块的哪一部分,它都会初始化您需要的变量,以便您以后可以进行处理(我在示例中使用了model)。你知道吗

同样重要的是要注意,在if块中,如果使用了if块的一部分(向下读取时找到的第一部分),那么其他所有elif语句也会被忽略。如果需要两个不相关的if语句,可以创建以下代码:

if statement1:
    #code here
elif statement2:
    #code not executed if statement1 is true

if statement3:
    #code executed, regardless of whether or not
    #either of the above statements are true or not

在本例中,语句1和2是一个if块的一部分,而语句3是另一个块的一部分。这也可能有助于修复代码中的一些问题。祝你在编码上好运,继续!你知道吗

相关问题 更多 >