Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

stupid python question

Name: beginner 2010-08-25 11:06

Simple (I think?) Python problem here.

Is there anyway to use a string the user inputs as a variable?

e.g., if a user inputs "var1", how can I then go and call something(var1) without using if/elif.

Here is a longer example of what I want to do; get the user to define "NATION" and then assign a random name from that nation's set of names.

http://pastebin.com/cHL9NqqX

The actual thing I'm writing will have tons of nationalities, so I'm hoping to skip the huge elif tree that would result.

Name: Anonymous 2010-08-25 11:17

I don't understand your question.a

Name: Anonymous 2010-08-25 11:24

In Python you can use the globals function to get a dictionary of variable names : values. But allowing user input in this scenario is too much of a security risk; rather, I'd advise you to create a nations dictionary, like so:

nationalities = {
    'canadian' : ["Gordy", "Gordon", "Gord"],
    'american' : ["Bob", "Bill", "Bush"]
}

and then you could simply do stuff like

nation = raw_input("Please enter the region:  ")
if nation.lower() not in nationalities:
    ....
names = nationalities[nation.lower()]
man = Naming(names)

but then, I don't see a point in using the Naming class, but I guess that you're just doing this as an exercise.

Name: Anonymous 2010-08-25 12:21

Thanks #3, I'm pretty sure that will work.

You're right that it's an exercise. The coded I posted there was just an example. The actual script is for a full character generation thingy for a corporate espionage strategy game, so the "name" will just be one aspect of the class as a whole. Since I want to be able to specify the ethnicity of a character (as well as just make 'random') in the class's __init__, I was worried I'd need potentially hundreds of if/elif statements to match the user's inputed ethnicity to the write grouping of names.

Name: Anonymous 2010-08-25 12:44

/prog/ is not stackoverflow, assholes.

Name: Anonymous 2010-08-25 12:54

For what it's worth, here's how I would do it:



from random import randint

Nations = {'us' : ["Uncle Sam", "Old McDorrad"],
'japan': ["Empra Hirihito", "Sage Samurai Miishima"]}

def getName(nation):
        name = Nations[nation][randint(0, len(Nations[nation])-1)]
        return name

class protoDude:
        def __init__(self):
                print "-------"
                Nation = raw_input("Enta Region: ").lower()
                self.name = getName(Nation)
                print "\n\nYOU ARE",self.name+"\n"

man = protoDude()

Name: Anonymous 2010-08-25 12:55

Saw this thread on /g/ an hour ago or so, everyone there were trying to sound like an expert and said ``Yeah definitely use raw_input, input is unsafe as it tries to run the command the user inputs'' instead of reading the OP.

Name: Anonymous 2010-08-25 13:14

>>5
So now it's me who's an asshole for helping someone? Give me a fucking break.

Name: Anonymous 2010-08-25 13:20

>>7

OP here. I refreshed the /g/ thread for a few minutes, and it seemed no one was posting so I figured I'd try here. I probably should have clarified that I'm using Python 3.1, but I figured that would have been obvious to anyone who knows Python and saw the print() statement; I started working with 2.7, so I know the difference between raw_input() and input() in the two.

Anyway, thanks for the help guys.

Name: Anonymous 2010-08-25 13:24

>>8
Yes, you are. Answering stupid questions only encourages more of them instead of getting the idiots to fuck off to places like /pr/.

Name: Anonymous 2010-08-25 13:32

>>10

You're right. If only this was yet another C vs. C++ thread, or a list of Sussman jokes.

Name: Anonymous 2010-08-25 13:41

>>11
You leave Him out of it!!!!

Name: Anonymous 2010-08-25 14:05

All questions about Python are stupid.

Name: Anonymous 2010-08-25 14:22

>>13
You are stupid.

Name: Anonymous 2010-08-25 19:32

>>9
/g/

now you have 2 problems

Don't change these.
Name: Email:
Entire Thread Thread List