我有以下代码:
#Previously defined variables
col_cp1_tariff_time_1_hour = 0
col_cp1_tariff_time_2_hour = 7
col_cp1_tariff_time_3_hour = 21
col_cp1_tariff_time_4_hour = 0
col_cp1_tariff_time_5_hour = 0
p1_time_variable_bands = 4
current_tariff_type = 1
time_cents_questions = [
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (hours[0]),
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (hours[1]),
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (hours[2]),
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (hours[3]),
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (hours[4])]
#Application
if current_tariff_type == 1: #Time of use
print("enter the corresponding tariffs.\nEnter for period 1: ")
hours = (col_cp1_tariff_time_1_hour,
col_cp1_tariff_time_2_hour,
col_cp1_tariff_time_3_hour,
col_cp1_tariff_time_4_hour,
col_cp1_tariff_time_5_hour)
variable_bands = []
for question in time_cents_questions[0:int(p1_time_variable_bands)-1]: #Change variable
try:
cents = int(input(question))
variable_bands.append(cents)
except (SyntaxError, ValueError):
variable_bands.append(0)
try:
[col_cp1_tariff_time_weekday_1_cents,
col_cp1_tariff_time_weekday_2_cents,
col_cp1_tariff_time_weekday_3_cents,
col_cp1_tariff_time_weekday_4_cents,
col_cp1_tariff_time_weekday_5_cents] = variable_bands
except (SyntaxError, ValueError):
variable_bands.append(0) #Change variable
正在触发错误: NameError:名称“ hours”未定义
是否可以将嵌套的“ hours”变量定义为time_cents_questions应该使用的hours变量?
答案 0 :(得分:0)
尝试此操作,在定义hours
之后进行字符串格式化:
#Previously defined variables
col_cp1_tariff_time_1_hour = 0
col_cp1_tariff_time_2_hour = 7
col_cp1_tariff_time_3_hour = 21
col_cp1_tariff_time_4_hour = 0
col_cp1_tariff_time_5_hour = 0
p1_time_variable_bands = 4
current_tariff_type = 1
time_cents_questions = [
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n",
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n",
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n",
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n",
"What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n"]
#Application
if current_tariff_type == 1: #Time of use
print("enter the corresponding tariffs.\nEnter for period 1: ")
hours = (col_cp1_tariff_time_1_hour,
col_cp1_tariff_time_2_hour,
col_cp1_tariff_time_3_hour,
col_cp1_tariff_time_4_hour,
col_cp1_tariff_time_5_hour)
variable_bands = []
for idx,question in enumerate(time_cents_questions[0:int(p1_time_variable_bands)-1]): #Change variable
try:
cents = int(input(question%hours[idx]))
variable_bands.append(cents)
except (SyntaxError, ValueError):
variable_bands.append(0)
try:
[col_cp1_tariff_time_weekday_1_cents,
col_cp1_tariff_time_weekday_2_cents,
col_cp1_tariff_time_weekday_3_cents,
col_cp1_tariff_time_weekday_4_cents,
col_cp1_tariff_time_weekday_5_cents] = variable_bands
except (SyntaxError, ValueError):
variable_bands.append(0) #Change variable