使用用户输入实例化 类
Instantiating classes with user input
我刚开始学习 类。在我正在学习的示例中,我注意到实例化的所有内容都是如何硬编码到示例中的。我想尝试弄清楚是否可以通过用户输入来实例化而无需执行此操作。
在第 74/75 行中,我的期望是 print(RecordID_map_PilotID[valUE].flownhours)
打印出我为特定实例选择记录的小时数。相反,我遇到了以下讨厌的错误:
Traceback (most recent call last):
File "oop_test.py", line 74, in <module>
RecordID_map_PilotID[valUE].recordflytime(loghours)
AttributeError: 'str' object has no attribute 'recordflytime'
任何人都可以帮助我理解为什么我打算 Python 做的事情实际上不起作用吗?
谢谢!
PilotID_ClassValCalls = {}
RecordID_map_PilotID = {}
class PilotRecord:
department = "Aviation"
asset = "Employee"
assetcategory = "FTE"
flownhours = 0
def __init__(self, pilotid, name, age, licensestatus, licenseexpiration, shiptype, callsign, flownhours):
self.pilotid = pilotid
self.name = name
self.age = age
self.licensestatus = licensestatus
self.licenseexpiration = licenseexpiration
self.shiptype = shiptype
self.callsign = callsign
self.flownhours = flownhours
def __str__(self):
return f"{self.pilotid} has an {self.licensestatus} license with an expiration date of {self.licenseexpiration} with the following callsigns:\n {self.callsign} ."
def recordflytime(self, hours):
self.flownhours = self.flownhours + hours
def Adding_Pilot_Records(): #This definitions created new pilot records and instantiates a new object for each pilot rcord that is created. In addition memory values are stored in Dict
add_records_number = int(input("How many pilot records would you like to add? "))
for eachrecord in range(add_records_number):
record_store = [input("Please provide pilot ID: "), input("Please provide pilot Name: "), int(input("Please provide pilot Age: ")),
input("Please provide pilot licensestatus: "), input("Please provide pilot licenseexpiration: "), input("Please provide pilot shiptype: "), input("Please provide pilot callsign: "), 0]
PilotID_ClassValCalls.update({eachrecord + 1 : record_store[0]})
RecordID_map_PilotID.update({PilotID_ClassValCalls[eachrecord+1]: record_store[0]})
PilotID_ClassValCalls[eachrecord+1] = PilotRecord(record_store[0], record_store[1], record_store[2], record_store[3], record_store[4], record_store[5], record_store[6], record_store[7])
while True == True:
print("Hello, Welcome to the PILOT RECORD DATABASE\n",
"What would you like to do with the Records?:\n\n",
" \t1 - \"Add\"\n",
" \t2 - \"Log\"\n",
" \t3 - \"Delete\"\n",
" \t4 - \"Quit\"\n")
userchoice = str(input().lower().strip())
try:
if userchoice == "1" or userchoice == "add":
Adding_Pilot_Records()
continue
elif userchoice == "2" or userchoice == "log":
while userchoice == "2" or userchoice == "log":
pickarecord = str(input("Which Record ID would you like to create a log for? ")).split()
pickarecord_yesno = input(f"Selected Record >>> {RecordID_map_PilotID[pickarecord[0]]}, Is this the correct record? [Y] [N] [Quit]").upper().split()
userchoice = ""
if pickarecord_yesno[0] == "Q" or pickarecord_yesno[0] == "QUIT":
break
elif pickarecord_yesno[0] == "Y" or pickarecord_yesno[0] == "YES":
userchoice = ""
loghours = int(input(f"How many hours would you like to log?"))
pickarecord = str(pickarecord[0])
for record, valUE in RecordID_map_PilotID.items():
if pickarecord in valUE:
RecordID_map_PilotID[valUE].recordflytime(loghours)
print(RecordID_map_PilotID[valUE].flownhours)
elif pickarecord_yesno[0] == "N" or pickarecord_yesno == "NO":
userchoice = "2"
continue
elif userchoice == "3" or userchoice == "delete":
continue
elif userchoice == "4" or userchoice == "quit":
break
except ValueError:
print("Sorry an Error has occurred")
这是导致错误的行:
RecordID_map_PilotID[valUE].recordflytime(loghours)
您正试图在 RecordID_map_PilotID[valUE]
上呼叫 .recordflytime()
。但是 RecordID_map_PilotID
是 str -> str
类型的字典,所以 RecordID_map_PilotID[valUE]
引用了一个字符串,而字符串没有 .recordflytime()
方法。
我可以看出它是一个字符串,因为这一行是唯一修改它的行:
RecordID_map_PilotID.update({PilotID_ClassValCalls[eachrecord+1]: record_store[0]})
因此,您正在用一个 dict
更新另一个 key/value,键为 PilotID_ClassValCalls[eachrecord+1]
,值为 record_store[0]
。 PilotID_ClassValCalls
的填充方式类似,其值一般也是record_store[0]
。你用调用 input()
的结果填充记录存储,这是一个字符串。
我建议您阅读 Python 中有关面向对象编程的一些示例 - 我认为您正在尝试做 'by hand' 使用现有的特定数据结构和方法做得更好在 Python.
中
更一般地说,将保存和操作数据的结构与获取进程输入的代码分开是个好主意。毕竟,您现在想通过直接用户输入来操作这些对象,但是如果您将内容保存到文件中并稍后再读回怎么办?或者从网页调用您的代码?您希望使用相同的 类,但不直接调用 input()
导致您的代码期望在控制台上输入。
我刚开始学习 类。在我正在学习的示例中,我注意到实例化的所有内容都是如何硬编码到示例中的。我想尝试弄清楚是否可以通过用户输入来实例化而无需执行此操作。
在第 74/75 行中,我的期望是 print(RecordID_map_PilotID[valUE].flownhours)
打印出我为特定实例选择记录的小时数。相反,我遇到了以下讨厌的错误:
Traceback (most recent call last):
File "oop_test.py", line 74, in <module>
RecordID_map_PilotID[valUE].recordflytime(loghours)
AttributeError: 'str' object has no attribute 'recordflytime'
任何人都可以帮助我理解为什么我打算 Python 做的事情实际上不起作用吗? 谢谢!
PilotID_ClassValCalls = {}
RecordID_map_PilotID = {}
class PilotRecord:
department = "Aviation"
asset = "Employee"
assetcategory = "FTE"
flownhours = 0
def __init__(self, pilotid, name, age, licensestatus, licenseexpiration, shiptype, callsign, flownhours):
self.pilotid = pilotid
self.name = name
self.age = age
self.licensestatus = licensestatus
self.licenseexpiration = licenseexpiration
self.shiptype = shiptype
self.callsign = callsign
self.flownhours = flownhours
def __str__(self):
return f"{self.pilotid} has an {self.licensestatus} license with an expiration date of {self.licenseexpiration} with the following callsigns:\n {self.callsign} ."
def recordflytime(self, hours):
self.flownhours = self.flownhours + hours
def Adding_Pilot_Records(): #This definitions created new pilot records and instantiates a new object for each pilot rcord that is created. In addition memory values are stored in Dict
add_records_number = int(input("How many pilot records would you like to add? "))
for eachrecord in range(add_records_number):
record_store = [input("Please provide pilot ID: "), input("Please provide pilot Name: "), int(input("Please provide pilot Age: ")),
input("Please provide pilot licensestatus: "), input("Please provide pilot licenseexpiration: "), input("Please provide pilot shiptype: "), input("Please provide pilot callsign: "), 0]
PilotID_ClassValCalls.update({eachrecord + 1 : record_store[0]})
RecordID_map_PilotID.update({PilotID_ClassValCalls[eachrecord+1]: record_store[0]})
PilotID_ClassValCalls[eachrecord+1] = PilotRecord(record_store[0], record_store[1], record_store[2], record_store[3], record_store[4], record_store[5], record_store[6], record_store[7])
while True == True:
print("Hello, Welcome to the PILOT RECORD DATABASE\n",
"What would you like to do with the Records?:\n\n",
" \t1 - \"Add\"\n",
" \t2 - \"Log\"\n",
" \t3 - \"Delete\"\n",
" \t4 - \"Quit\"\n")
userchoice = str(input().lower().strip())
try:
if userchoice == "1" or userchoice == "add":
Adding_Pilot_Records()
continue
elif userchoice == "2" or userchoice == "log":
while userchoice == "2" or userchoice == "log":
pickarecord = str(input("Which Record ID would you like to create a log for? ")).split()
pickarecord_yesno = input(f"Selected Record >>> {RecordID_map_PilotID[pickarecord[0]]}, Is this the correct record? [Y] [N] [Quit]").upper().split()
userchoice = ""
if pickarecord_yesno[0] == "Q" or pickarecord_yesno[0] == "QUIT":
break
elif pickarecord_yesno[0] == "Y" or pickarecord_yesno[0] == "YES":
userchoice = ""
loghours = int(input(f"How many hours would you like to log?"))
pickarecord = str(pickarecord[0])
for record, valUE in RecordID_map_PilotID.items():
if pickarecord in valUE:
RecordID_map_PilotID[valUE].recordflytime(loghours)
print(RecordID_map_PilotID[valUE].flownhours)
elif pickarecord_yesno[0] == "N" or pickarecord_yesno == "NO":
userchoice = "2"
continue
elif userchoice == "3" or userchoice == "delete":
continue
elif userchoice == "4" or userchoice == "quit":
break
except ValueError:
print("Sorry an Error has occurred")
这是导致错误的行:
RecordID_map_PilotID[valUE].recordflytime(loghours)
您正试图在 RecordID_map_PilotID[valUE]
上呼叫 .recordflytime()
。但是 RecordID_map_PilotID
是 str -> str
类型的字典,所以 RecordID_map_PilotID[valUE]
引用了一个字符串,而字符串没有 .recordflytime()
方法。
我可以看出它是一个字符串,因为这一行是唯一修改它的行:
RecordID_map_PilotID.update({PilotID_ClassValCalls[eachrecord+1]: record_store[0]})
因此,您正在用一个 dict
更新另一个 key/value,键为 PilotID_ClassValCalls[eachrecord+1]
,值为 record_store[0]
。 PilotID_ClassValCalls
的填充方式类似,其值一般也是record_store[0]
。你用调用 input()
的结果填充记录存储,这是一个字符串。
我建议您阅读 Python 中有关面向对象编程的一些示例 - 我认为您正在尝试做 'by hand' 使用现有的特定数据结构和方法做得更好在 Python.
中更一般地说,将保存和操作数据的结构与获取进程输入的代码分开是个好主意。毕竟,您现在想通过直接用户输入来操作这些对象,但是如果您将内容保存到文件中并稍后再读回怎么办?或者从网页调用您的代码?您希望使用相同的 类,但不直接调用 input()
导致您的代码期望在控制台上输入。