EvilZone
Programming and Scripting => Scripting Languages => : 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.
-
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 :)
-
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?
-
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?
-
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)
-
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.
-
When i tried to use that code, it didn't work for me. I'm using python 3.3. Is that the reason?
-
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.
-
Ok, i see. I'll try it out.
-
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.
-
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.