if-else语句中的最短方法

时间:2019-10-28 13:12:40

标签: python-3.7

我需要提高自己的编码技能。这是因为当我编写程序时,教授希望我的程序的时间效率降低。 需要缩短这段代码,谢谢:))

我已经尝试过使用OR,AND并在for循环中使用两个或多个变量。

 def sum_resistor(self):
     num1 = ""
     num2 = ""
     self.omega_1 = (self.R1.get())
     self.omega_2 = (self.R2.get())

     for i in range(len(self.omega_1)):
         if (self.omega_1[i].isdigit()): 
             num1 = num1 + self.omega_1[i]

     for j in range(len(self.omega_2)):
         if (self.omega_2[j].isdigit()): 
             num2 = num2 + self.omega_2[j]

     if 'T' in self.omega_1:
         self.omega_1 = ((float(num1)*1e12))
     elif 'G' in self.omega_1:
         self.omega_1 = ((float(num1)*1e9))
     elif 'M' in self.omega_1:
         self.omega_1 = ((float(num1)*1e6))
     elif 'k' in self.omega_1:
         self.omega_1 = ((float(num1)*1e3))
     elif 'm' in self.omega_1:
         self.omega_1 = ((float(num1)*1e-3))
     elif 'u' in self.omega_1:
         self.omega_1 = ((float(num1)*1e-6))
     elif 'n' in self.omega_1:
         self.omega_1 = ((float(num1)*1e-9))
     elif 'p' in self.omega_1:
         self.omega_1 = ((float(num1)*1e-12))
     elif self.omega_1 == '':
         self.omega_1 = 0


     if 'T' in self.omega_2:
         self.omega_2 = ((float(num2)*1e12))
     elif 'G' in self.omega_2:
         self.omega_2 = ((float(num2)*1e9))
     elif 'M' in self.omega_2:
         self.omega_2 = ((float(num2)*1e6))
     elif 'k' in self.omega_2:
         self.omega_2 = ((float(num2)*1e3))
     elif 'm' in self.omega_2:
         self.omega_2 = ((float(num2)*1e-3))
     elif 'u' in self.omega_2:
         self.omega_2 = ((float(num2)*1e-6))
     elif 'n' in self.omega_2:
         self.omega_2 = ((float(num2)*1e-9))
     elif 'p' in self.omega_2:
         self.omega_2 = ((float(num2)*1e-12))
     elif self.omega_2 == '':
         self.omega_2 = 0

0 个答案:

没有答案