0

I am still in the "any technology sufficiently advanced..." phase of learning to program. This code seems clunky, and hard to update if needed. Is there a better way to go about this? It just looks completely bonkers to me, but it works... so yay?

It takes a variable from TKinter radial 1-16, and uses it select the grade1 ect variable. The grade1 variable then link to sheet names on gspread.

def go():
    subject = v.get()
    if  subject == 1:
        subject = grade1
    elif  subject == 2:
        subject = grade2    
    elif  subject == 3:
        subject = grade3
    elif  subject == 4:
        subject = grade4  
    elif  subject == 5:
        subject = grade5
    elif  subject == 6:
        subject = grade6
    elif  subject == 7:
        subject = grade7
    elif  subject == 8:
        subject = grade8
    elif  subject == 9:
        subject = grade9
    elif  subject == 10:
        subject = grade10    
    elif  subject == 11:
        subject = grade11
    elif  subject == 12:
        subject = grade12 
    elif  subject == 13:
        subject = grade13
    elif  subject == 14:
        subject = grade14
    elif  subject == 15:
        subject = grade15
    elif  subject == 16:
        subject = grade16
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Calvin Hobbes
  • 77
  • 1
  • 7

4 Answers4

9

Do not keep 16 different variables for grade**! Use one list:

grades = [1, 2, 3, ...]  # or whatever your values are

Then all you need to do is:

subject = grades[v.get() - 1]
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thank you, this worked. I have done something similar with other things, but this did not clue in for me. Amazing thanks. – Calvin Hobbes Oct 30 '18 at 02:22
  • 2
    As a rule of thumb: whenever you attach a `…2` to any variable name, you should pause and think about what you're actually doing. – deceze Oct 30 '18 at 02:34
  • so for my TK radial buttons can I do something similar? but1= Radiobutton(root,text=grade[0],padx=5, variable=v, value =1,command= go) but2= Radiobutton(root,text=grade[1],padx=5, variable=v, value =2,command= go) but3= Radiobutton(root,text=grade[2],padx=5, variable=v, value =3,command= go) – Calvin Hobbes Oct 30 '18 at 02:52
  • Sure… `buttons = [Radiobutton(...), Radiobutton(...), ...]`. – deceze Oct 30 '18 at 02:54
2

Instead of defining 16 separate grade1...grade16 variables, you could store them in a list like grades=[grade1,...,grade16] and then set subject as subject = grades[v.get()-1]

samlli
  • 106
  • 5
1

Or use dictionary:

d={1:grade1,2:grade2 ... 16:grade16}
print(d[v.get()])

You will get desired grade.

You have to have a dictionary with keys of all possible v.get() values, and values of the grades you want for each key, so then you get the v.get() key in the dictionary, then get it's value, by using d[v.get()], now you've got the desired grade.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    is there a big difference between a list and dictionary? or is just the indexing is inside of the dictionary? – Calvin Hobbes Oct 30 '18 at 02:44
  • 1
    @CalvinHobbes It's actually not indexing it's getting key, then it's value, https://docs.python.org/3/tutorial/datastructures.html#dictionaries – U13-Forward Oct 30 '18 at 02:51
  • 1
    When iterating through a list it's `O(n)` However, a dictionary is `O(1)` because it `dictionary` uses `hash` – Seungho Lee Oct 30 '18 at 06:06
0

There are few ways to get around it.

# 1. Using the `list` data type
def go():
    grade_list = [grade1, grade2, ... grade16]
    subject = [grade_list[v.get() - 1]]


# 2. Using the `eval` 
def go():
       subject = v.get()
       if subject in range(1, 17):
           subject = eval("grade" + str(subject))

The first method is considered better code. However, there is another way of using eval keyword. It treats the code inside as an expression. Since only the number part of the variable changes. However, eval is not the best way I recommend. For more, refer to this page Why is using 'eval' a bad practice?

Seungho Lee
  • 1,068
  • 4
  • 16
  • 42