我收到一个非常奇怪的错误。在一个循环中调用函数Choose_units()。它在第一次调用时有效,但是第二次给出此错误。错误在“ units_used = UNITS.get(current_units)[0]”行上 我在行之前使用了一些检查,它显示current_units!= None。我们被要求在分配中使用current_units作为全局变量。
我在该行之前进行了一些检查,结果表明current_units!= None。但是它仍然显示相同的错误,并且由于某种原因不在第一次迭代中显示。
while True:
print_menu();
try:
choice = int(input("What is your choice?: "));
except:
print("Please enter an integer only");
continue;
if choice == 1:
Process_a_new_data_file(current_set);
elif choice == 2:
Choose_units();
def Choose_units():
global current_units
if current_units is not None:
print("a")
print(current_units)
units_used = UNITS.get(current_units)[0]
print("Current units in " + units_used)
print("Choose new units:\n")
for i in UNITS:
print(str(i) + " - " + UNITS[i][0])
while True:
current_units = input("Which unit?\n")
for i in UNITS:
if(int(current_units) == i):
return
print("Please choose a unit from the list")
continue
它应该工作而不显示错误。
我的样品运行:
Main Menu
---------
1 - Process a new data file
2 - Choose units
3 - Edit room filter
4 - Show summary statistics
5 -Show temperature by date and time
6 -Show histogram of temperatures
7 - Quit
What is your choice?: 2
a
0
Current units in Celsius
Choose new units:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
5 - Rankine
Which unit?
1
Main Menu
---------
1 - Process a new data file
2 - Choose units
3 - Edit room filter
4 - Show summary statistics
5 -Show temperature by date and time
6 -Show histogram of temperatures
7 - Quit
What is your choice?: 2
a
1
Traceback (most recent call last):
File "Assignment9.py", line 274, in <module>
main()
File "Assignment9.py", line 253, in main
Choose_units();
File "Assignment9.py", line 79, in Choose_units
units_used = UNITS.get(current_units)[0]
TypeError: 'NoneType' object is not subscriptable
答案 0 :(得分:1)
我了解的是 UNITS.get(current_units)的值不是列表或数组。该值将变为“无”。由于这个问题尚不清楚UNITS方法。要求您检查UNITS方法。