-1

Is there any way to make the following code more elegant? meaning, like in a few lines instead.

            if beta == 'a':
                a = E3632A
            if beta == 'b':
                b = E3632A
            if beta == 'c':
                c = E3632A
            if beta == 'd':
                d = E3632A
            if beta == 'e':
                e = E3632A
            if beta == 'f':
                f = E3632A
            if beta == 'g':
                g = E3632A
            if beta == 'h':
                h = E3632A
            if beta == 'i':
                i = E3632A
            if beta == 'j':
                j = E3632A
    average=(a+b+c+d+e+f+g+h+i+j)/10
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
F Casian
  • 93
  • 1
  • 11
  • Hi FCasisan, welcome to SO! Usually, working code that could be prettier isn't a great fit for stack overflow, so if you don't get the response you're looking for here I might try Code Review instead. Also, I'm a fan of using dictionaries for something like this.. also... what does that last line do? – en_Knight Nov 04 '19 at 21:23
  • 4
    Possible duplicate of [Replacements for switch statement in Python?](https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) – Joseph Sible-Reinstate Monica Nov 04 '19 at 21:23
  • Hi @en_Knight My code pics a power supply voltage & current consumption during 120 seconds period every 12 seconds to calculate the voltage/current and get the average of system. The results will tell me if energy consumption is correct or not. – F Casian Nov 04 '19 at 21:28
  • Thank you @JosephSible but the link you share is not what I'm looking for. Right now I'm reviewing the Ruzihm response, probably that's exactly what I'm looking for. – F Casian Nov 04 '19 at 22:07
  • @FCasian did you find a solution to this? – Ruzihm Jan 14 '20 at 03:10
  • @Ruzihm Hi there Ruzihm, yes I did, – F Casian Jun 15 '20 at 16:18

2 Answers2

2

Here's a way using exec, which will work but is not recommended due to exec being generally an error-prone method of doing things:

import string
if beta in string.ascii_lowercase[:10]:
    exec("%s = E3632A" % beta)
average=(a+b+c+d+e+f+g+h+i+j)/10

A better approach is to assign your variables to a dictionary:

vars = {'a':a, 
        'b':b, 
        'c':c,
        'd':d,
        'e':e,
        'f':f,
        'g':g,
        'h':h,
        'i':i,
        'j':j
         # other vars ...
          } 

import string
if beta in string.ascii_lowercase[:10]:
    vars[beta] = E3632A
average=sum(vars.values())/len(vars.values())  
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 1
    I think, you can remove the IF block. So that it can match with any value in variable. – roshan ok Nov 04 '19 at 23:10
  • Also , in the second example , dictionary would throw a nameerror , as the a,b,c,d... are not defined – roshan ok Nov 04 '19 at 23:16
  • 1
    @roshanok which one are you talking about for removing the IF block? Asker does not want to alter anything when `beta` is not a-j. They may be using other variables in other ways they have not shown in their question. For the nameerror, the variables are pretty obviously already defined before the snippet Asker shows. – Ruzihm Nov 04 '19 at 23:19
  • I agree with you and totally Understand. Its a suggestion and information to the asker - if it can help there usecase. – roshan ok Nov 04 '19 at 23:27
1

Assigned an example value

E3632A = 10

for i in range(ord('a'), ord('j')+1):
    globals()[chr(i)] = E3632A
        
avergae = (a+b+c+d+e+f+g+h+i+j)/10
print(avergae)

we have to make a character as a variable in this case i guess , so we may have to use global in this case , If this is in a class we could use settattr

class example: 
    def method_example(self):
       E3632A = 10
    
       for i in range(ord('a'), ord('j')+1):
         setattr(self, chr(i), E3632A)
            
       avergae = (a+b+c+d+e+f+g+h+i+j)/10
       print(avergae)
Community
  • 1
  • 1
DMS
  • 31
  • 3
  • The first only works if a-j are global variables. If Asker's code snippet occurs within a function, then it will not work. The second sets all variables a-j to `E3632A`, which is not what Asker wants to do. – Ruzihm Nov 04 '19 at 23:24
  • As the question it self is about assigning the variable whose name matches a string so I thought may be he is looking string to act as variable so my answer emphasize s on how to make string as variable. I just try to work on the example, so I used a to j, anyways asker only can choose wat he wants. Thanks for the review though – DMS Nov 05 '19 at 01:44