Author Topic: user created variables  (Read 5257 times)

0 Members and 1 Guest are viewing this topic.

Offline IrishYoga

  • Serf
  • *
  • Posts: 31
  • Cookies: -2
    • View Profile
user created variables
« on: 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.

Offline Uriah

  • Sir
  • ***
  • Posts: 454
  • Cookies: 42
  • άξονας
    • View Profile
Re: user created variables
« Reply #1 on: 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 :)
« Last Edit: February 05, 2013, 07:46:52 am by Uriah »

Mike245

  • Guest
Re: user created variables
« Reply #2 on: 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?

Offline IrishYoga

  • Serf
  • *
  • Posts: 31
  • Cookies: -2
    • View Profile
Re: user created variables
« Reply #3 on: 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?

Offline DaNePaLI

  • Peasant
  • *
  • Posts: 55
  • Cookies: 12
  • Forever n00b
    • View Profile
Re: user created variables
« Reply #4 on: 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:

Code: [Select]
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)

Code: [Select]
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)

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: user created variables
« Reply #5 on: 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:

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

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

Code: [Select]
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.
« Last Edit: February 05, 2013, 09:20:12 pm by techb »
>>>import this
-----------------------------

Offline IrishYoga

  • Serf
  • *
  • Posts: 31
  • Cookies: -2
    • View Profile
Re: user created variables
« Reply #6 on: 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?

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: user created variables
« Reply #7 on: 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.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline IrishYoga

  • Serf
  • *
  • Posts: 31
  • Cookies: -2
    • View Profile
Re: user created variables
« Reply #8 on: February 08, 2013, 05:11:30 am »
Ok, i see. I'll try it out.

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: user created variables
« Reply #9 on: 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.
-Xires

Offline IrishYoga

  • Serf
  • *
  • Posts: 31
  • Cookies: -2
    • View Profile
Re: user created variables
« Reply #10 on: 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.