variables - How to vary an object's name in a Python 3 loop?

I'm trying to create a simple calculator, and I need to vary what my object's name is in a loop.

RoR = RoR + 1
"C{0}R{1}".format(CoC, RoR) = int(input("What is your row {0} Column {1} number?: ".format(RoR, CoC)))

This is what I tried, and it did not work. I want the objects to look like C1R1 = 18 C1R2 = 88 etc. I'm not very good at Python, so some help would be very much appreciated!

1 Answer

  1. Donald- Reply

    2019-11-14

    Use a dictionary as mentioned in Woobles comment!

    mydict = {
               'a' : 1,
               'b' : 2,
              ...
             }
    

    It's easy to extract/modify the data of 'a' in 'mydict' :

    >>>print( mydict['a'] )
    1
    >>>mydict['a'] = 2
    >>>print( mydict['a'] )
    2
    

    So in your case it would be something like:

    mydict = {}
    RoR = RoR + 1
    
    mydict["C{0}R{1}".format(CoC, RoR)] = int(input("What is your row {0} Column {1} number?:  ".format(RoR, CoC)))
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>