name = input("what is your name ")
file_name = str(input("What do you want to name this .txt file\n> "))
if file_name[-4:] != ".txt":
file_name += ".txt"
询问那里的姓名和员工姓名
print("Why hello",name,"now lets caculate that employee's next pay check")
def employees():
emplist = []
while True:
names = input('What is the name of the employee')
if names == 'done':
break
else:
emplist += [names]
print(emplist)
pay(emplist)
def pay(emplist):
for person in emplist:
print("now i need hourly pay of",person,)
pay = float(input("> "))
print("now i need the hours worked by",person,)
hours = float(input("> "))
做数学
if hours > 40:
over = 1.50
overtimeR = over * pay
overtime = overtimeR * (hours-40)
hours += 40
else:
overtime = 0
尚未完成
if overtime > 0:
hours2 = 40
totalpay = (pay * hours2) + overtime
pay_without_overtime = pay * hours2
else:
totalpay = (pay * hours) + overtime
person_2 = ""
person_2 += person
info = ("Employee: "+str(person_2)+"\nTotal Hours: "+str(hours))
with open(file_name, 'a+')as file_data_2:
file_data_2.append(info)
employees()
我该如何解决
AttributeError: '_io.TextIOWrapper' object has no attribute 'append'
当你打开一个带有with open(file_name, 'a+') as file_data_2:
变量的文件时,它file_data_2
会成为类_io.TextIOWrapper 的一个实例,它确实没有这样的属性。如果您想查看可用于您创建的任何变量的属性/方法,您可以在 Python 的交互模式中轻松完成。打开终端,运行你的 Python(在我的例子中是 Python 3):
$ python3
首先,打开您的文件并将其存储在一个变量中,类似于您在代码中所做的:
>>> file = open("sample.txt", 'a+')
该变量file
现在是_io.TextIOWrapper
类的一个实例。您可以使用以下命令检查该类的可用方法:
>>> dir(file)
这是输出:
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__',
'__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__',
'__init__', '__iter__', '__le__', '__lt__', '__ne__', '__new__',
'__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '_checkClosed',
'_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing',
'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno',
'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines',
'read', 'readable', 'readline', 'readlines', 'seek', 'seekable',
'tell', 'truncate', 'writable', 'write', 'writelines']
如您所见,没有“追加”方法。但是,有“写”,我想这就是您所需要的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句