+ 3

How to Create objects based on one of the user inputs required for the class and access the attributes using the object?

I have this python code: class Student: #create a class def __init__(self,name,roll,percentage): self.name=name self.roll=roll self.percentage=percentage Now I want to send attributes of the class Student from a user input using a function like this: def Input(): name=input("please enter your name\n") roll=input("please enter your roll\n") percentage=input("please enter your percentage\n") Now the problem is I want to create an object to the class Student based on name variable and send attributes to the class something like this: name=Student(name,roll,percentage) And when I call it with "name",It should show the result like this: #for example: name="Rahul" roll="405" percentage="80" and when I type Rahul.percentage,it should display 80. Please Help me.

11th May 2018, 6:20 AM
®️ahul Mathews™️
®️ahul Mathews™️ - avatar
7 Answers
+ 2
Rahul Mathews -here you go. This code won't run properly in SL, though, because of the known input limitations of the ide https://code.sololearn.com/chiuXlPwk7xD/?ref=app
11th May 2018, 11:21 AM
Johannes
Johannes - avatar
+ 2
You are quite close - check out here: https://code.sololearn.com/cTkJPtK1pXsh/?ref=app
11th May 2018, 7:52 AM
Johannes
Johannes - avatar
+ 1
Johannes what if we have to input multiple persons of the same details and create multiple instances.
11th May 2018, 8:44 AM
®️ahul Mathews™️
®️ahul Mathews™️ - avatar
+ 1
Kuba Siekierzyński Just tagged you so that you can help.Thanks in advance.
11th May 2018, 8:45 AM
®️ahul Mathews™️
®️ahul Mathews™️ - avatar
+ 1
Johannes how do we create multiple objects(different) when we keep in loop
11th May 2018, 9:57 AM
®️ahul Mathews™️
®️ahul Mathews™️ - avatar
0
Rahul Mathews - in SL, you can only have 1 input at the beginning of the program. In a different editor, you could put the input line into a while loop. So, it would continue to prompt for input until the condition is false.
11th May 2018, 9:00 AM
Johannes
Johannes - avatar
0
You could use a dict with index as key (like a uid) and the value as list consisting of name, roll, and percentage.
11th May 2018, 9:13 AM
Johannes
Johannes - avatar