0

Say I have a variable, 'New', who's value is Raw_input("Enter name: ") How could I take this variable and pass it into a class?

4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Your_mum
0

What do you mean 'pass it to a class'? Can you post some code and pin-point your problem please?

0

You can pass value in as argument.

new = raw_input('Enter name: ')

class Test(object):
    def __init__(self,value):
        self.value = value

x = Test(new)
print x.value

'''
output-->
Enter name: hi
hi
'''
0

Not really recommended, since you don't really always want to pause your code but I think you want this:

class Name:
    def __init__(self):
        self.name = raw_input("Name: ")

However, I'd have to agree that snippsat's solution is a better written class, because if neccessary you could use it on names that you don't get through raw_input like...a list! For example:

#snippseat's class
class Test(object):
    def __init__(self,value):
        self.value = value

for name in namelist:
    test = Test(name)
0

Thanks, I can't see why I couldn't work that out!
neway, problem solved.

This topic has been dead for over six months. 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.