Reputation: 55
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
Upvotes: 2
Views: 8249
Reputation: 29302
I'd say that there are a couple of problems in your code:
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
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
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
.
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.
Upvotes: 1
Reputation: 86240
In the ask method, each time name
is written, it should be self.name
.
For example:
while name == "":
# to
while self.name == "":
In your main function, object1.createname()
will give an error. It's expecting two parameters. The implicit, called object1 outside the function, and self
inside, is being passed. You don't, however, give a value for name
.
object1.createname("Joe")
These lines are only executed when name is a blank string. So print '\n'
will do the same thing. Leaving it out will just make one "Name:" on each line until valid input is given.
else:
print self.name # "self." added
Consider defining an init method, which takes a name. That way you can set one when the class is created. You can also give a default value, which won't affect your current calling (it's optional, so if you want to give a name later, you can.
def __init__(self, name="SomeDefaultName"):
pass # Content here
Upvotes: 1