0

The problem is getting that when I give the user input value to as an argument, it takes as a string and gives an error.

class Employee():
    def details(self,name=[]):
        print(
          "Name of Employee is:",name[0],
          "\nSalary of Employee is:",name[1],
          "\nPost of Employee is:",name[2],
          "\nLocation of Employee is:",name[3]
        )    

harry = ["Harry",10000,"Engineer","Gurgoan"]
manish = ["Manish",20000,"Manager","Noida"]
e = Employee()
f = input("Enter name to get details:")
e.details(f)

if I use e.details(harry) and don't use input function it works fine. but I want to get detail of harry by using the input function.

OKEE
  • 450
  • 3
  • 15
Harshit
  • 13
  • 3
  • Use a dictionary: `d = {"harry": harry, ...}` and then `e.details(d[f])` – tobias_k Jul 07 '21 at 11:26
  • `f = input("Enter name to get details").split()` to get 'f' as a list and then pass it to `e.details(f)`. Keep in mind that if length of 'f' is lower than 4 you will get an IndexError – berkeebal Jul 07 '21 at 11:38
  • 1
    This is not related to your question, but don't use mutable default arguments (i.e. `name = []`). Check https://stackoverflow.com/q/1132941/4046632 As to your question - better find a good tutorial on OOP. Name, salary, post, location should be attributes of `Employee` class and probably passed as arguments when create instance of the class. – buran Jul 07 '21 at 11:48

3 Answers3

4

When you create an object from the Employee class and then call from it details function, e object - does not know about your lists that you define before object creation

I am not sure what your code is doing, but I think you meant something like this:

class Employee:
    def __init__(self):
        self.workers_data = {
            "harry": ["Harry", 10000, "Engineer", "Gurgoan"],
            "manish": ["Manish", 20000, "Manager", "Noida"],
        }

    def details(self, name):
        print(
            "Name of Employee is: {}\nSalary of Employee is: {}\nPost of Employee is: {}\nLocation of Employee is: {}".format(
                *self.workers_data[name]
            ),
        )


e = Employee()
f = input("Enter name to get details:")
e.details(f)
OKEE
  • 450
  • 3
  • 15
2

This is because your string can be less than 4 symbol’s, and when you call name[3] it returns error. Also if you want to get words from input, you can split it by space’s: input().split()

If you need to get info about name, try to use dictionary:

harry = ["Harry",10000,"Engineer","Gurgoan"]
manish = ["Manish",20000,"Manager","Noida"]
Names = {"Harry": harry, "Manish": manish}
e = Employee()
f = input("Enter name to get details:")
e.details(Names[f])
Lakerr
  • 97
  • 4
-1

Just use eval at the last line to completely fix the problem

I tested & it worked just now

e.details(eval(f)) # eval the string 

eval makes string as variable name

Edit: But use it at your own risk user can run anything with this method

mwxgaf
  • 66
  • 1
  • 9
  • Your welcome; please tap the check mark and also arrow up of the answer to show that worked for people how see this Q&A later – mwxgaf Jul 07 '21 at 11:49
  • 3
    Never ever put user input in eval(). It's a huge security risk. This will work but it is very bad practice to do so. There are much better solutions given to this question – Almog-at-Nailo Jul 07 '21 at 11:49