![](/img/trans.png)
[英]How can I separate the displays and variables in each of my tabs in PyQt?
[英]It displays that one of my variables is not defined when from what i can see it is
这是出现的错误:
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])
跟踪代码中发生的事情有点困难,但从我所见,看起来您从未创建变量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...
注意如何全部投入到现在同样的问题的所有通道进入相同的变量,因此,无论是什么,如果块的一部分被调用时,它总是会初始化你需要的变量,这样就可以以后再处理(我用的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 是另一个块的一部分。 这也可能有助于解决代码中的一些问题。 祝你编码好运,并坚持下去!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.