Hi.. I would like to know how to create a variable while the program is being executed. Here's what should happen.
The program asks for an input from the user and then creates a variable who's name is the value of the input and then assign's a value to it.

for example say the user gave the input "country".
The program should take that input and create a variable called country... and optionally assign a value to it.

Recommended Answers

All 15 Replies

Okay, I will, but first give me one reasonable use for this technique.

You wouldn't have a pointer to the contents of that variable, unless you had it in a list or something...
The only reason I can think of for doing this is that you would want to keep two values in one varable. Why not just use a list? (eg. country = )

Maybe give more details so we know why you want to do that?

Hi.. I would like to know how to create a variable while the program is being executed. Here's what should happen.
The program asks for an input from the user and then creates a variable who's name is the value of the input and then assign's a value to it.

for example say the user gave the input "country".
The program should take that input and create a variable called country... and optionally assign a value to it.

Can be done, but is a very bad idea! If the user puts in a name that already exists, programmer's hell could break loose.

Good point.. I didn't think of that.
I'm building a script to keep track of the scores of a cricket match... I want the user to be able to enter a name and then the program should create a instance of a predefined class with that name.
For example if the user input "Sri_Lanka", then the program should create this

Sri_Lanka = my_predefined_class()

I asked about creating a variable because I though the creating a variable and a class would involve similar procedures.

You could use a dictionary

class Country(object):
    def __init__(self, name):
        self.name = name

def create_countries():
    countriesDict = dict()
    print("Please enter the countries names (enter 'done' to exit the loop): ")
    while True:
        userInput = raw_input("country: ").strip() # or input if python 3.x
        if userInput:
            if userInput == "done":
                break
            countriesDict[userInput] = Country(userInput)
    return countriesDict

theCountries = create_countries()
print(theCountries)
commented: Thanks for the example.. they really help with questions like these. +1

I'm pretty new to OOP.
what does it mean when "object" is provided as an argument to a class?

class Country(object):

what does it mean when "object" is provided as an argument to a class?

It means that the base class of your class is 'object'. Depending on your version of python it's necessary or not to declare a base class. My advice is to declare object until nobody uses python 2.x.

so that's not necessary in python 3.x? Maybe that's why I didn't know about it.
and just to confirm I understand you previous example..
your program stores instances of the class country in (name,value) pairs of the dictionary doesn't it? the name is the input and the value is the actual object...

your program stores instances of the class country in (name,value) pairs of the dictionary doesn't it? the name is the input and the value is the actual object...

exactly.

Thanks for your help.. that works for me....
However, if anyone knows how to actually generate a variable which has the name of an input provided, please post a code snippet in this thread. Hopefully, someone will be able to learn something.

Thanks for your help.. that works for me....
However, if anyone knows how to actually generate a variable which has the name of an input provided, please post a code snippet in this thread. Hopefully, someone will be able to learn something.

(emphasis mine)

No, they won't. You see, there's no good reason anybody would want to do that. I guarantee you that anybody wanting to do this has a totally different underlying problem to which they have ignorantly attached their own silly possible solution.

Can be done, but is a very bad idea! If the user puts in a name that already exists, programmer's hell could break loose.

Couldn't you write an if statement to check if the name is already in use? If it is, it will restart and say the variable exists, else, it would continue.

(emphasis mine)

No, they won't. You see, there's no good reason anybody would want to do that. I guarantee you that anybody wanting to do this has a totally different underlying problem to which they have ignorantly attached their own silly possible solution.

well, at least they'll learn why it's a bad idea...

Well, it's a bad idea UNLESS the right precautions are taken :icon_wink: .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.