EvilZone

Programming and Scripting => Scripting Languages => : IrishYoga February 05, 2013, 06:59:48 AM

: user created variables
: IrishYoga February 05, 2013, 06:59:48 AM
So if i don't want to continue to make variables for the user to use, how can i make the user input values into new variables that have not already been created? I was thinking of a tuple that consists of a-z and that the user could input values into those, but i'm not sure how it could be done.
: Re: user created variables
: Uriah February 05, 2013, 07:39:48 AM
In what language?
Python, for example, could be something like:
x = raw_input(" ")
You just have to set a variable to the result of the user input.
Or do you mean that you want the user to create the name of the variable their self?
Name of the programming language and more details might be helpful :)
: Re: user created variables
: Mike245 February 05, 2013, 07:56:00 AM
We need some more information? I'm guessing Python because of tuples... And I'm not exactly sure what you are trying to accomplish. Could you be a little more specific?
: Re: user created variables
: IrishYoga February 05, 2013, 02:23:41 PM
yeah,sorry the code is python. I meant that if i'm making a grade calculator, i don't want to keep making variables, and i'm not even sure how much grades the user will have. I want them to be able to input as much grades as they want without me having to make a new variable for each one. Is it possible?
: Re: user created variables
: DaNePaLI February 05, 2013, 02:58:30 PM
I don't quite understand you. So you want to build a house without bricks, stones, cements, and other basic things you must have if you need to make a practically usable home? Or what?

Anyway, if I understood you right, you can create an empty list & then append:

:
grades = []
and then, using the appropriate loop for you, you would then do:

[/code]grades.append()[/code]

Or you could use the array (I'm assuming you'll have float values here)

:
from array import array

grades = array('f')
print "Enter grades below(Once finished, enter N)"

while True:
        grade = raw_input()
        if grade == 'N' or grade == 'n':
                break;
        grades.append(int(grade))

for grade in grades:
        print grade

Btw, I'm not the good python coder so others might have something to contribute in this. & Sorry if I misunderstood your question (Its not quite understandable)
: Re: user created variables
: techb February 05, 2013, 09:18:56 PM
I don't quite understand you. So you want to build a house without bricks, stones, cements, and other basic things you must have if you need to make a practically usable home? Or what?

Anyway, if I understood you right, you can create an empty list & then append:

:
grades = []
and then, using the appropriate loop for you, you would then do:

:
grades.append()
Or you could use the array (I'm assuming you'll have float values here)

:
from array import array

grades = array('f')
print "Enter grades below(Once finished, enter N)"

while True:
        grade = raw_input()
        if grade == 'N' or grade == 'n':
                break;
        grades.append(int(grade))

for grade in grades:
        print grade

Btw, I'm not the good python coder so others might have something to contribute in this. & Sorry if I misunderstood your question (Its not quite understandable)

This is on the right track.

@op Use lists, because tuples are immutable. If you want labels such as a to z, use a dict.
: Re: user created variables
: IrishYoga February 07, 2013, 02:03:04 AM
When i tried to use that code, it didn't work for me. I'm using python 3.3. Is that the reason?
: Re: user created variables
: vezzy February 07, 2013, 02:06:28 AM
Yes. If you're using Python 3.x, you need to put print statements in parentheses and I believe raw_input() was renamed simply to input().

It's good to know the differences, as most code is still written in 2.x.
: Re: user created variables
: IrishYoga February 08, 2013, 05:11:30 AM
Ok, i see. I'll try it out.
: Re: user created variables
: Xires February 08, 2013, 09:20:47 AM
I suggest hash tables.  They're more generalized and available in almost any language.  They also tend to be rather quick for randomly accessing data.
: Re: user created variables
: IrishYoga February 09, 2013, 03:34:59 AM
Ok thanks for helping. You guys understood my question, i'm just way too under-qualified to understand anything that has been suggested. I will try to learn more python, and come back to this post.