I am very new to programming in general, and of course, it follows: to Python. Yet, I am in desperate need of a particular database program, and I am attempting to write it via objects, since I need to learn the ways of OOP anyway. I am currently trying to write a function that will take a user's input as variable 'type' and change the object's attribute of type 'type' to another user input...

type = str(raw_input("type: "))
entry = str(raw_input("entry: "))
self.type = entry

where type in self.type is changed to whatever was entered in the beginning.

I could use a bunch of if statements, but that would get long and unwieldy, and not really very reusable.

8 Years
Discussion Span
Last Post by petersrin

Your best method would be to use lists (in C, arrays).
1. I don't think you can use self outside of a class.
2. raw_input always returns a string

data = []
type = raw_input("Type: ")
entry = raw_input("Entry: ")
def report(data):
    x = len(data)
    print data
    if x > 1: print data[0:x]
    else: pass

Also avoid using type, since that is a builtin Python function name.

You could keep track of your input in a dictionary:

d = {}

# input value and variable name
d[raw_input('Enter variable name: ')] = raw_input('Your value: ')

print d, d.keys(), d.values()

I am not quite sure where you want to go with this, it doesn't make much sense! If the user enters the variable name, and picks a Python keyword or function name, you get into all sorts of errors!


Thanks to ZZucker: that is what I needed, it seems. Just to let everyone know, I'm essentially creating predefined dictionary entries, then iterating through them with a for loop to fill them with user input. Dictionary was exactly what I needed.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.