Python Forum
Using input function in a Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using input function in a Class
#1
Hi All, I am trying to make the code below to be a little bit more interactive by doing the following:
1. When run the code, it will show you the list of all robots
2. Then there is an input function to ask for robot's name
3. after typing in the robot's name, it will use the selfintroduce function to display its attributes

Any help is appreciated. thanks!

class Robot():
    def __init__ (self, name, color, weight): #defining attributes name, color, weight
        self.name = name
        self.color = color
        self.weight = weight
    def selfintroduce(self): #defining selfintroduce function
        print("The robot name is", self.name)
        print("The color is",self.color)
        print("The weight is",self.weight)

robot1 = Robot("Zero","white","100lb")
robot2 = Robot("R2D2", "blue", "200lb")

c=input("enter robot's name\n") 
print(c.selfintroduce(self)) #doesnt work 

#robot1.selfintroduce()
#robot2.selfintroduce()
Reply
#2
Hi :)
The input function returns a string, so when you are asked for the robot and you type "Zero" the value of c would be "Zero", nothing more. So it is not the Object that you created a few lines before that. The string does not have the selfintroduce function, since it is not an Object of the class Robot. Also when you call the methods of an object you do not provide the self attribute. This is the reference to itself and is filled automatically.

To achiev your goal and to make it easy you could use dictionaries:
class Robot():
    def __init__ (self, name, color, weight): #defining attributes name, color, weight
        self.name = name
        self.color = color
        self.weight = weight
    def selfintroduce(self): #defining selfintroduce function
        print("The robot name is", self.name)
        print("The color is",self.color)
        print("The weight is",self.weight)

robots = {"zero": Robot("Zero","white","100lb"), "r2d2": Robot("R2D2", "blue", "200lb")}

print("The robots names are: " + ", ".join(robots.keys()))
c=input("enter robot's name\n") 
robots[c.lower()].selfintroduce()
As keys I used the lowered version of the name, because even if the user inputs "zero" it would be kind of right. You do not need to use the print on to the selfintroduce method, because you are printing everything inside of the method. Also you are not returning any value therefore the print function surrounding the call of the selfintroduce function would print "None"
Reply
#3
Thank you so much for the concise explanation!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,015 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  The function of double underscore back and front in a class function name? Pedroski55 9 620 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  class Update input (Gpio pin raspberry pi) caslor 2 787 Jan-30-2023, 08:05 PM
Last Post: caslor
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,073 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Showing an empty chart, then input data via function kgall89 0 973 Jun-02-2022, 01:53 AM
Last Post: kgall89
  TimeOut a function in a class ? Armandito 1 1,644 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  input function question barryjo 12 2,704 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,078 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,808 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  Problem with input after function luilong 10 4,072 Dec-04-2021, 12:16 AM
Last Post: luilong

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020