-7

I am designing a game in python. Lets say i have 5 levels and a variable m. if i am on level 1, m will be 101 if i am on level 2, m will be 102 and so on

Now, i also have a variables for best score for all levels, lets say b_101,b_102,b_103,b_104,b_105. All initially set to 0.

Now while playing i have a variable current_score. I want to update best score on meeting some condition.

if(condition met):
   global m
   update_score(m)

def update_score(m):
    if(m==101):
        b_101=current_score
    elif(m==102):
        b_102=current_score
    elif(m==103):
        b_103=current_score
    elif(m==104):
        b_104=current_score
    elif(m==105):
        b_105=current_score

Now in actual, i may have 100 levels and i want my update_score function to be small and not write 100 if else lines.

I want it be be something like:

def update_score(m):
    b_{m}=current_score

I know above code in incorrect, but i want something b_xxx to be updated on basis of m. Is it possible in python.

In perl, it can be done

${'b_'.$m} = $current_score;
Saurabh Shrivastava
  • 1,394
  • 4
  • 21
  • 50
  • 5
    how about using a `dict` with the variables as keys? – Lawrence Benson Sep 10 '15 at 14:44
  • 1
    Given your question's current wording, it's impossible to know why you need variables named after their values, but it's highly likely there's no good reason for it. I'd use Morgan Thrapp's code as a base and work out how best to use it to solve your actual problem. – Rob Grant Sep 10 '15 at 14:56
  • I may be playing any level like 101 or 105, when my game finishes, i want t update score for that level only. So if m=101, i want to update variable b_101 and if m=105, i want to update b_105 – Saurabh Shrivastava Sep 10 '15 at 15:00
  • 5
    Use a dict. This is blindingly obvious, which is why it's being recommended. Class also a decent option, depending on what you need. Not this. Almost never this. – Rob Grant Sep 10 '15 at 15:16

2 Answers2

8

Just use a dictionary.

def update_score(m, score, scores = {}):
    scores[m] = score
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
  • variable names for best score are fixed, like b_101. Can you modify your code accordingly. – Saurabh Shrivastava Sep 10 '15 at 14:47
  • I'm not sure what you mean. You don't need the individual `b_10x` variables any more, you just use the dictionary. – Morgan Thrapp Sep 10 '15 at 14:54
  • i mean, i cant change my variables now. Please if possible,provide a solution not alternative. – Saurabh Shrivastava Sep 10 '15 at 14:58
  • @user3388005 Why can't you? – Morgan Thrapp Sep 10 '15 at 14:58
  • because it will require a lot of code change in my existing app. If this small thing can be done in Perl, i am sure there is a possible code in python too. – Saurabh Shrivastava Sep 10 '15 at 15:02
  • 3
    @user3388005 Your options are, rewrite it now and take a little more time to have a good long term solution, or continue to write bad code. Up to you. – Morgan Thrapp Sep 10 '15 at 15:03
  • i will rewrite it, if you can just say that what i need cannot be done in python because in perl, it can be done. – Saurabh Shrivastava Sep 10 '15 at 15:07
  • 5
    Oh, it can be done in Python in a way similar to the Perl way, by using `exec`. But it's **really** not a good idea to create code that's littered with a zillion separate variables like that, _especially_ when you use dynamic variable creation to access them, no matter which language you're writing in. It's _much_ better to organize your data using the various data structures that the language provides. – PM 2Ring Sep 10 '15 at 15:25
  • Thanks everyone for all the solutions....i think, continuing with my if else logic is best for me... – Saurabh Shrivastava Sep 10 '15 at 15:51
  • respectfully, i asked best way to reach my destination by bike and people answered that i should use car instead, bikes are not save :) and people who tried to answer to my question got downvotes. – Saurabh Shrivastava Sep 10 '15 at 16:11
  • @user3388005 No, a better analogy is you asking how to get from the USA to China via bike and getting mad when people tell you to use a boat. Try reading about [X/Y Problems](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Morgan Thrapp Sep 10 '15 at 16:12
  • Anyways, you have assumed that my destination is thousand of miles away, but actually it is only few miles. Again Thanks a lot for your time and advice. – Saurabh Shrivastava Sep 10 '15 at 16:16
  • Found the best solution using globals()[name]=current_score – Saurabh Shrivastava Sep 11 '15 at 05:38
-2

I think a class is better. So u have unlimited number of levels. It is too easier to modify

class ScoreStore:
  def __init__(self, max_level=100):
    for i in range(101, max_level + 1):
      setattr(self, "b_{}".format(i), None)
    self.current_score = None

  def update_score(self, m):
    setattr(self, "b_{}".format(m + 100), self.current_score)

  def get_score_of_level(self, m):
    return getattr(self, "b_{}".format(m + 100), self.current_score)

But dict ist easiest

julivico
  • 1,107
  • 1
  • 7
  • 4
  • 1
    Why are you using dynamic attributes in the class? Just add a scores dict in `__init__`. – Morgan Thrapp Sep 10 '15 at 15:02
  • is a good idea too. attributes of a class will be stored like a dict too. But you can call the attribute like s.b_102. Class and Attribute is in large project easier to scale than dict – julivico Sep 10 '15 at 15:09