python - 输入重复代码(Python)

标签 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= 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")

为什么没有成功?

最佳答案

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

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

关于python - 输入重复代码(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24043344/

相关文章:

python - Django 列表在 View 之间不会被破坏

python - 查找给定字符串是python中的单词,字符还是数字

javascript - Node JS - 如何在同一 session 中逐一运行跨浏览器测试?

java - 如何在selenium的testng中静态打开浏览器(所有测试都在一个浏览器中)

python - 对于 Electron 应用程序,可以将 Selenium 与 Python 一起使用吗?

python - 获取 np.linalg.svd 的奇异值作为矩阵

python - 将元数据添加到 WSGIRequest 对象

用于大型 HTML/XML 的 Python 模板

powershell - 将新文件夹添加到某个位置时触发脚本

ios - 寻找有关使用 Appium 自动化真实 iOS 设备的教程