我一直在制作一个用于比较两个时间表的python程序,但我遇到了很多这个问题。变量,文本和其他所有内容的命名将非常重复,例如,每周输入12345123451234512345,共5课。有一种简单的方法可以实现自动化吗?
这是我的计划:
m1= input("M1")
m2= input('M2')
m3= input('M3')
m4= input('M4')
m5= input('M5')
t1= input('T1')
t2= input('T2')
t3= input('T3')
t4= input('T4')
t5= input('T5')
w1= input('W1')
w2= input('W2')
w3= input('W3')
w4= input('W4')
w5= input('W5')
th1= input('TH1')
th2= input('TH2')
th3= input('TH3')
th4= input('TH4')
th5= input('TH5')
f1= input('F1')
f2= input('F2')
f3= input('F3')
f4= input('F4')
f5= input('F5')
am1= input('1M1')
am2= input('1M2')
am3= input('1M3')
am4= input('1M4')
am5= input('1M5')
at1= input('1T1')
at2= input('1T2')
at3= input('1T3')
at4= input('1T4')
at5= input('1T5')
aw1= input('1W1')
aw2= input('1W2')
aw3= input('1W3')
aw4= input('1W4')
aw5= input('1W5')
ath1= input('1TH1')
ath2= input('1TH2')
ath3= input('1TH3')
ath4= input('1TH4')
ath5= input('1TH5')
af1= input('1F1')
af2= input('1F2')
af3= input('1F3')
af4= input('1F4')
af5= input('1F5')
pbm1= input("M1")
pbm2= input('M2')
pbm3= input('M3')
pbm4= input('M4')
pbm5= input('M5')
pbt1= input('T1')
pbt2= input('T2')
pbt3= input('T3')
pbt4= input('T4')
pbt5= input('T5')
pbw1= input('W1')
pbw2= input('W2')
pbw3= input('W3')
pbw4= input('W4')
pbw5= input('W5')
pbth1= input('TH1')
pbth2= input('TH2')
pbth3= input('TH3')
pbth4= input('TH4')
pbth5= input('TH5')
pbf1= input('F1')
pbf2= input('F2')
pbf3= input('F3')
pbf4= input('F4')
pbf5= input('F5')
pbam1= input('1M1')
pbam2= input('1M2')
pbam3= input('1M3')
pbam4= input('1M4')
pbam5= input('1M5')
pbat1= input('1T1')
pbat2= input('1T2')
pbat3= input('1T3')
pbat4= input('1T4')
pbat5= input('1T5')
pbaw1= input('1W1')
pbaw2= input('1W2')
pbaw3= input('1W3')
pbaw4= input('1W4')
pbaw5= input('1W5')
pbath1= input('1TH1')
pbath2= input('1TH2')
pbath3= input('1TH3')
pbath4= input('1TH4')
pbath5= input('1TH5')
pbaf1= input('1F1')
pbaf2= input('1F2')
pbaf3= input('1F3')
pbaf4= input('1F4')
pbaf5= input('1F5')
if(m1==pbm1):
print("Same for week 1 Monday Lesson 1")
if(m2==pbm2):
print("Same for week 1 Monday lesson 2")
if(m3==pbm3):
print("Same for week 1 Monday Lesson 3")
if(m4==pbm4):
print("Same for week 1 Monday Lesson 4")
if(m5==pbm5):
print("Same for week 1 Monday Lesson 5")
if(t1==pbt1):
print("Same for week 1 Tuesday Lesson 1")
if(t1==pbt2):
print("Same for week 1 Tuesday Lesson 2")
if(t1==pbt3):
print("Same for week 1 Tuesday Lesson 3")
if(t1==pbt4):
print("Same for week 1 Tuesday Lesson 4")
if(t1==pbt5):
print("Same for week 1 Tuesday Lesson 5")
if(w1==pbw1):
print("Same for week 1 Wednesday Lesson 1")
if(w2==pbw2):
print("Same for week 1 Wednesday Lesson 2")
if(w3==pbw3):
print("Same for week 1 Wednesday Lesson 3")
if(w4==pbw4):
print("Same for week 1 Wednesday Lesson 4")
if(w5==pbw5):
print("Same for week 1 Wednesday Lesson 5")
if(th1==pbth1):
print("Same for week 1 Thursday Lesson 1")
if(th2==pbth2):
print("Same for week 1 Thursday Lesson 2")
if(th3==pbth3):
print("Same for week 1 Thursday Lesson 3")
if(th4==pbth4):
print("Same for week 1 Thursday Lesson 4")
if(th5==pbth5):
print("Same for week 1 Thursday Lesson 5")
if(f1==pbf1):
print("Same for week 1 Friday Lesson 1")
if(f2==pbf2):
print("Same for week 1 Friday Lesson 2")
if(f3==pbf3):
print("Same for week 1 Friday Lesson 3")
if(f4==pbf4):
print("Same for week 1 Friday Lesson 4")
if(f5==pbf5):
print("Same for week 2 Friday Lesson 5")
if(am1==pbam1):
print("Same for week 2 Monday Lesson 1")
if(am2==pbam2):
print("Same for week 2 Monday lesson 2")
if(am3==pbam3):
print("Same for week 2 Monday Lesson 3")
if(am4==pbam4):
print("Same for week 2 Monday Lesson 4")
if(am5==pbam5):
print("Same for week 2 Monday Lesson 5")
if(at1==pbat1):
print("Same for week 2 Tuesday Lesson 1")
if(at1==pbat2):
print("Same for week 2 Tuesday Lesson 2")
if(at1==pbat3):
print("Same for week 2 Tuesday Lesson 3")
if(at1==pbat4):
print("Same for week 2 Tuesday Lesson 4")
if(at1==pbat5):
print("Same for week 2 Tuesday Lesson 5")
if(aw1==pbaw1):
print("Same for week 2 Wednesday Lesson 1")
if(aw2==pbaw2):
print("Same for week 2 Wednesday Lesson 2")
if(aw3==pbaw3):
print("Same for week 2 Wednesday Lesson 3")
if(aw4==pbaw4):
print("Same for week 2 Wednesday Lesson 4")
if(aw5==pbaw5):
print("Same for week 2 Wednesday Lesson 5")
if(ath1==pbath1):
print("Same for week 2 Thursday Lesson 1")
if(ath2==pbath2):
print("Same for week 2 Thursday Lesson 2")
if(ath3==pbath3):
print("Same for week 2 Thursday Lesson 3")
if(ath4==pbath4):
print("Same for week 2 Thursday Lesson 4")
if(ath5==pbath5):
print("Same for week 2 Thursday Lesson 5")
if(af1==pbaf1):
print("Same for week 2 Friday Lesson 1")
if(af2==pbaf2):
print("Same for week 2 Friday Lesson 2")
if(af3==pbaf3):
print("Same for week 2 Friday Lesson 3")
if(af4==pbaf4):
print("Same for week 2 Friday Lesson 4")
if(af5==pbaf5):
print("Same for week 2 Friday Lesson 5")
你现在看到了吗?
第二次尝试不起作用
m_input = {}
for i in range(1, 6):
m_input['M{}'.format(i)] = input('input M{}: '.format(i))
bm_input = {}
for i in range(1, 6):
m_input['bM{}'.format(i)] = input('input bM{}: '.format(i))
if (m_input['M1']==bm_input['bM1']):
print("Same for week 1 Monday Lesson 1")
if (m_input['M2']==bm_input['bM2']):
print("Same for week 1 Monday Lesson 1")
if (m_input['M3']==bm_input['bM3']):
print("Same for week 1 Monday Lesson 1")
if (m_input['M4']==bm_input['bM1']):
print("Same for week 1 Monday Lesson 1")
if (m_input['M5']==bm_input['bM1']):
print("Same for week 1 Monday Lesson 1")
为什么不起作用?
答案 0 :(得分:2)
您可以尝试使用数组而不是所有变量和for循环来添加数据:
for i in range(5)
myarray.append("m" + str(i+1))
答案 1 :(得分:1)
等碎片
m1= input("M1")
m2= input('M2')
m3= input('M3')
m4= input('M4')
m5= input('M5')
可以通过使用循环并将输入存储在列表中来缩短。
m_input = []
for i in range(1, 6):
m_input.append(input('input M{}: '.format(i)))
您可以在x
访问输入号码m_input[x-1]
。或者,您可以使用字典来创建名称和输入值对。
m_input = {}
for i in range(1, 6):
m_input['M{}'.format(i)] = input('input M{}: '.format(i))
现在,您可以M1
访问m_input['M1']
,M2
访问m_input['M2']
,依此类推。