Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:45:35+00:00 2026-05-31T12:45:35+00:00

I have made a simple, test program to experiment using classes. It creates a

  • 0

I have made a simple, test program to experiment using classes. It creates a “person” class, which takes the arguments of name and age. I am now trying to create an interactive program that allows the user to manage a list of people, like a register or address book. They can create a new person, as shown in the function below.

def createnew():
    newname = input("What is their name?")
    newage = input("How old are they?")

    newperson = Person(newname, newage)

Of course the problem with the above is it assigns newname literally rather than the string value of “newname”. So I tried

newperson.lower() = Person(newname, newage)
str(newperson) = Person(newname, newage)
newperson[0:] = Person(newname, newage)

but they all return the error, can’t assign to function call.
My question is how do I access the value of the variable newname and assign it to the Person, or any other class.

Note: I am a n00b/newb, meaning I have not come from another language, and am following a book. Please explain any answers given if they are not ridiculously obvious.

Many Thanks.

To make it slightly clearer, I am trying to make the user-inputted value for the new person’s name the object name ( in my example it is newperson )

Update:

Thanks for all the answers, I gather it’s not a great idea to allow the user to set variable names so I re-jigged my code. I made a dictionary which after the user has created a new person, writes the data given by the class str function to the key of the given name. While this means I can’t then access the object later on, because newperson will have been overwritten, it allows me to build a list of people with a set of data which is what I wanted to do in the first place, I just mistakingly thought it would be easier to set the object name.
Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:45:36+00:00Added an answer on May 31, 2026 at 12:45 pm

    If I understand you correctly, what you want is to create the Person object, then create a global variable with the name being the name of the person.

    This is bad.

    What you really want is a way to store people objects and retrieve them later by their names. So, use a dictionary:

    people = {}
    
    def createnew():
        ...
        people[newname] = Person(newname, newage)
    

    Then later, you can access the Person in the dictionary:

    print(people['name of the person'])
    

    (I’m assuming that you’re using Python 3, since you’re using input() rather than raw_input() as in Python 2.x)

    If you really must create a new variable (which is bad, since people’s names aren’t usually valid Python identifiers), use the globals() function:

    >>> globals()['foo'] = 2
    >>> foo
    2
    

    but I include this for the sake of completeness. Don’t use this!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple program with : working with files(read write) end class
I have made a simple test application for the issue, two winforms each containing
I have made my own file type (.ddd) and I made a simple program
I have started using jQuery and rails. I have made a simple form that
I have a simple nav made up of the following HTML; <div class=inner> <div
I'm learning java and I have made simple program that simply reads value from
I have a made a simple program that reproduces the problem: #include <boost/archive/text_oarchive.hpp> #include
I have made a super simple test case of a problem I'm having with
I have a fairly simple program based largely on a simple bluetooth test client
I have made a simple example application to test Fluent NHibernate with the automapping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.