【发布时间】:2018-01-23 12:02:08
【问题描述】:
我有一段简单的代码,您可以在其中输入姓名以接收有关此人的更多信息。例如,您输入“john”以获取有关 John 的信息。但是,例如,如果用户输入“John”或“JOHN”,我会收到以下回溯错误。
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
John
NameError: name 'John' is not defined
我已经能够将.lower() 插入到我的代码中的位置(当前已删除)而没有错误,但它仍然不接受“John”/“JOHN”等,并以小写形式返回有关 John 的所有信息 - 其中不是我想要的。
是否可以允许用户使用大写字母输入?我对 Python 很陌生,很欣赏这是非常直接的,我已经看到类似的线程与 .lower() 和 input().lower() 回复和类似的,但努力找出将其包含在我的代码中以允许用户输入“约翰”等。谢谢。
class Employees:
def __init__(self, name, lastname, age, department):
self.name = name
self.lastname = lastname
self.age = age
self.department = department
def displayEmployees(self):
return("The employee is called " + self.name + ' ' + self.lastname + " and is " + str(self.age) + " years old, and works in the " + self.department + " department.")
employee1 = Employees("Sam", "Smith", 25, "Office")
employee2 = Employees("John", "Smith", 23, "Admin")
employee3 = Employees("Amy", "Smith", 28, "Admin")
employee4 = Employees("Alice", "Smith", 30, "Reception")
employee5 = Employees("Chris", "Smith", 51, "Managers")
sam = employee1.displayEmployees()
john = employee2.displayEmployees()
amy = employee3.displayEmployees()
alice = employee4.displayEmployees()
chris = employee5.displayEmployees()
print("Please input the first name of the employee you wish to find out more about the employee")
【问题讨论】:
标签: python python-3.x input lowercase