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

  • Home
  • SEARCH
  • 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 7184365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:11:12+00:00 2026-05-28T18:11:12+00:00

I’m still learning how to work within classes. I’m trying to get user input

  • 0

I’m still learning how to work within classes. I’m trying to get user input from the user using self methods (Training exercises).

Here is my code (I just want to get the user input and display it, but with using methods)

import os
import time

class Programs: 
        def basicintro(self):
                print "Welcome to the BMR Calculator"
                time.sleep(1)      
        def createname(self,name):
                self.name = name
        def ask(self):
                while name == "":
                        name = raw_input("Name:")
                        if name != "":
                                print "Hello", name
                                time.sleep(1)
                        else:
                                print name                
def main():
        object1 = Programs()
        object1.basicintro()
        object1.createname()
        object1.ask()    
        raw_input("\n\nPress enter to exit.")          
main()

I basically just want to ask the user to input their name and then make their name an object so if i need it later in the program, I can just call it

  • 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-28T18:11:12+00:00Added an answer on May 28, 2026 at 6:11 pm

    I’d say that there are a couple of problems in your code:

    1. First you need a good input cycle, I’ll use a simple one:

      while 1: # infinite loop
          name = raw_input("Name: ")
          if name == "":
              print "Ops! Retry"
          else:
              print "Hello", name
              break  # this will break the loop
      
    2. Let’s put this cycle inside your class:

      class Programs:
      
          def ask(self):
              while 1: # infinite loop
                  name = raw_input("Name: ")
                  if name == "":
                      print "Ops! Retry"
                  else:
                      print "Hello", name
                      break  # this will break the loop
      
    3. For what I understand you want to store the name provided by the user as an attribute of of your Program class instance, so:

      class Programs:
      
          def __init__(self):  # class constructur (called at creation time)
              self.name = ""   # the default name is the empty string
      
          def ask(self):
              while 1: # infinite loop
                  name = raw_input("Name: ")
                  if name == "":
                      print "Ops! Retry"
                  else:
                      print "Hello", name
                      break  # this will break the loop
      
              self.name = name  # assign to self.name the value name
      

      Example:

      >>> prog = Programs()
      >>> prog.name
      ''
      >>> prog.ask()
      Name: Rik
      Hello Rik
      >>> prog.name
      'Rik'
      

      As you can see the method __init__ was called immediatly and placed assigned the empty string to prog.name.

    4. If you want to put that example code inside your script, this is how to do it:

      class Programs:
      
          def __init__(self):  # class constructur (called at creation time)
              self.name = ""   # the default name is the empty string
      
          def ask(self):
              while 1: # infinite loop
                  name = raw_input("Name: ")
                  if name == "":
                      print "Ops! Retry"
                  else:
                      print "Hello ", name
                      break  # this will break the loop
      
              self.name = name  # assign to self.name the value name
      
      
      if __name__ == '__main__':
          prog = Programs()
          prog.ask()
          raw_input("\n\nPress enter to exit.")
      

      If you want, you can put that code inside a function, but what I wanted to show the use of if __name__ == '__main__':. Information can be found in this well answered question.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.