How do I use an input to print a class definition?
Question:
I am trying to code something basic in python just for fun and I encountered an issue,
# Employee is a class with the initialization being self, name, status
e1 = Employee("Tom, Lincoln", "Present")
e2 = Employee("Sam, Bradley", "Absent")
print(e1.status)
# printing e1 status will make "Present" or "Absent"
while True:
try:
cmd = input("Cmd: ")
if cmd == "status_check":
who = input("Who: ")
# putting in e1 or e2 will get their respective statuses
I’ve tried everything I can think off like, making it so that it gets a number out of the input("Who: ")
input so I can better use eval or exac, but doing that makes it so I cant run e1.status
because all it has is a 1 and I can’t make a "e" appear in front of it so I can’t run e1.status
. I’ve also tried using just eval or exac but that didn’t get the wanted result because I would have to type my code in the input("Cmd: ")
. That’s isn’t the only things I’ve tried but those are some that come to mind.
I’m just stumped here.
Answers:
If you want to map names to values, you don’t want separate variables. You want a dictionary.
Rather than
e1 = Employee("Tom, Lincoln", "Present")
e2 = Employee("Sam, Bradley", "Absent")
Consider
employees = {
'e1': Employee("Tom, Lincoln", "Present"),
'e2': Employee("Sam, Bradley", "Absent"),
}
To access an employee, write
print(employees["e1"].status)
and then you can use the input string to subscript employees
as well.
who = input("Who: ")
print(employees[who].status)
one other approach is to use sys module:
import sys
# class definitions etc
while True:
try:
cmd = input("Cmd: ")
if cmd == "status_check":
who = input("Who: ")
atr = getattr(sys.modules[__name__], who)
print(atr.status)
I am trying to code something basic in python just for fun and I encountered an issue,
# Employee is a class with the initialization being self, name, status
e1 = Employee("Tom, Lincoln", "Present")
e2 = Employee("Sam, Bradley", "Absent")
print(e1.status)
# printing e1 status will make "Present" or "Absent"
while True:
try:
cmd = input("Cmd: ")
if cmd == "status_check":
who = input("Who: ")
# putting in e1 or e2 will get their respective statuses
I’ve tried everything I can think off like, making it so that it gets a number out of the input("Who: ")
input so I can better use eval or exac, but doing that makes it so I cant run e1.status
because all it has is a 1 and I can’t make a "e" appear in front of it so I can’t run e1.status
. I’ve also tried using just eval or exac but that didn’t get the wanted result because I would have to type my code in the input("Cmd: ")
. That’s isn’t the only things I’ve tried but those are some that come to mind.
I’m just stumped here.
If you want to map names to values, you don’t want separate variables. You want a dictionary.
Rather than
e1 = Employee("Tom, Lincoln", "Present")
e2 = Employee("Sam, Bradley", "Absent")
Consider
employees = {
'e1': Employee("Tom, Lincoln", "Present"),
'e2': Employee("Sam, Bradley", "Absent"),
}
To access an employee, write
print(employees["e1"].status)
and then you can use the input string to subscript employees
as well.
who = input("Who: ")
print(employees[who].status)
one other approach is to use sys module:
import sys
# class definitions etc
while True:
try:
cmd = input("Cmd: ")
if cmd == "status_check":
who = input("Who: ")
atr = getattr(sys.modules[__name__], who)
print(atr.status)