键入重复代码(Python)

键入重复代码(Python),python,automation,Python,Automation,我一直在制作一个python程序来比较两个时间表,但我经常遇到这个问题。变量的命名、文本和其他所有内容都将是非常重复的,例如每周在5节课中输入12345123451234512345。有没有一种简单的方法来实现自动化 这是我的节目: 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= i

我一直在制作一个python程序来比较两个时间表,但我经常遇到这个问题。变量的命名、文本和其他所有内容都将是非常重复的,例如每周在5节课中输入12345123451234512345。有没有一种简单的方法来实现自动化

这是我的节目:

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")
为什么它不起作用?

碎片,例如

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)))        
您可以在
m_input[x-1]
访问输入编号
x
。或者,您可以使用字典创建名称和输入值对

m_input = {}
for i in range(1, 6):
    m_input['M{}'.format(i)] = input('input M{}: '.format(i))

现在,您可以在
m_input['M1']
处访问
M1
M2
m_input['M2']
处访问
M1
等等。

您可以尝试使用数组而不是所有变量和for循环来添加数据:

for i in range(5)
    myarray.append("m" + str(i+1))

什么是重复的,写程序还是输入时间表?如果程序的编写是重复的,那么你就没有正确地进行编程。如果你想得到关于如何做不同的意见,请复制并粘贴输入和程序的示例。现在的问题是,唯一可能的答案是“是的,有一种自动化的方法;是的,对一些人来说很容易,但对其他人来说却不是那么容易”。我在原始问题中添加了我的代码。每当你发现自己一遍又一遍地重复同样的事情时,几乎可以肯定有更好的方法。也称为“干”(不要重复自己)原则。使用
“M{}”。格式(i+1)
更具可读性,并且不需要显式强制转换
i+1
。请检查我的原始问题,以了解我使用此语法生成的代码,并解释它不起作用的原因。@SamRob85新代码的问题如下:在第二个
for
循环中,您正在将
bM
值存储在
m\u输入
字典中。将
m_input['bM{}.format(i)]=input('input bM{}:'.format(i))
更改为
bM_input['bM{}.format(i)]=input('input bM{}:'.format(i))
。谢谢您的帮助