Python 正在写入 .txt 文件
Python writing a .txt file
print('Welcome, I am Sam. I am ABMs personal assistant, today we will be pooling tips together!')
file = open(“tipsdate.txt”,”w”)
name=str(input('Who do I have the pleasure of working with? '))
tips=int(input('How many tips did we collect today? '))
workers=int(input('How many people worked today? '))
cars=int(input('How many cars did we park today?'))
if workers == 1:
print('Worker 1 earned $',tips)
elif workers == 2:
a=int(input('How many hours did worker 1 work? '))
b=int(input('How many hours did worker 2 work? '))
totalhrs=a+b
tipsa= (a/totalhrs)* tips
tipsb= (b/totalhrs)*tips
print('Worker one earned $',tipsa)
print('Worker two earned $',tipsb)
elif workers == 3:
a=int(input('How many hours did worker 1 work? '))
b=int(input('How many hours did worker 2 work? '))
c=int(input('How many hours did worker 3 work? '))
totalhrs=a+b+c
tipsa= (a/totalhrs)* tips
tipsb= (b/totalhrs)*tips
tipsc= (c/totalhrs)*tips
print('Worker one earned $',tipsa)
print('Worker two earned $',tipsb)
print('Worker three earned $',tipsc)
elif workers == 4:
a=int(input('How many hours did worker 1 work? '))
b=int(input('How many hours did worker 2 work? '))
c=int(input('How many hours did worker 3 work? '))
d=int(input('How many hours did worker 4 work? '))
totalhrs=a+b+c+d
tipsa= (a/totalhrs)* tips
tipsb= (b/totalhrs)*tips
tipsc= (c/totalhrs)*tips
tipsd= (d/totalhrs)*tips
print('Worker one earned $',tipsa)
print('Worker two earned $',tipsb)
print('Worker three earned $',tipsc)
print('Worker four earned $', tipsd)
elif workers > 4:
print('I am not programmed to calculate tips beyond 4 workers, I am a work in progress!')
cartips=tips/cars
print('Our tips per car average today was $ ',cartips)
file.close()
print('Thank you for your help',name,'I hope we work together again soon! :) ')
您好,感谢您花时间帮助我。我是一个业余的、自学成才的 python 编码员。我正在尝试弄清楚如何编写带有条件循环和各种用户输入的 .txt 文件。我意识到这是菜鸟,但如何才能将所有这些信息存储在一个 .txt 文件中呢?
这是我的 valet 停车工作,只是一个有趣的副项目,可以帮助我获得更多经验!谢谢你。也欢迎任何建议,以改进我的代码——我意识到它可能会尖叫菜鸟!
file.write
而不是 print
会将 if 写入文件。 input
也写入监视器而不是文件。检查 https://docs.python.org/3/tutorial/inputoutput.html
你不需要为每个工人重复代码,相反你可以做这样的事情 loops variable sized workers
:
tips = 100
workers = [("w1", 8), ("w2", 8)]
hours = 0
with open("scratch.log", "w") as f:
for worker, hour in workers:
hours = hours + hour
print(hours)
f.write("%s\n" % str(hours))
for worker, hour in workers:
tip = (float(tips) / float(hours)) * hour
print(worker, tip)
f.write("%s %s\n" % (worker, str(tip)))
整体代码为:
newline = '\n'
name = str(input('Name? '))
tips = int(input('Total Tips? '))
workers = int(input('Total Workers Today? '))
cars = int(input('Cars Parked Today? '))
worker_hours = []
total_hours = 0
for idx, worker in enumerate(range(workers)):
count = idx + 1
hours = int(input('How many hours did worker %s work? ' % count))
worker_hours.append(('w' + str(count), hours))
with open("tips.log", "w") as f:
for worker, hours in worker_hours:
total_hours = total_hours + hours
print(total_hours)
f.write("%s" % str(total_hours))
f.write(newline)
for worker, hours in worker_hours:
tip = (float(tips) / float(total_hours)) * hours
print(worker, tip)
f.write("%s %s" % (worker, str(tip)))
f.write(newline)
cartips = tips/cars
print('Tips per car today was $%s.' % cartips)
f.write('Tips per car today was $%s.' % cartips)
f.write(newline)
print('Welcome, I am Sam. I am ABMs personal assistant, today we will be pooling tips together!')
file = open(“tipsdate.txt”,”w”)
name=str(input('Who do I have the pleasure of working with? '))
tips=int(input('How many tips did we collect today? '))
workers=int(input('How many people worked today? '))
cars=int(input('How many cars did we park today?'))
if workers == 1:
print('Worker 1 earned $',tips)
elif workers == 2:
a=int(input('How many hours did worker 1 work? '))
b=int(input('How many hours did worker 2 work? '))
totalhrs=a+b
tipsa= (a/totalhrs)* tips
tipsb= (b/totalhrs)*tips
print('Worker one earned $',tipsa)
print('Worker two earned $',tipsb)
elif workers == 3:
a=int(input('How many hours did worker 1 work? '))
b=int(input('How many hours did worker 2 work? '))
c=int(input('How many hours did worker 3 work? '))
totalhrs=a+b+c
tipsa= (a/totalhrs)* tips
tipsb= (b/totalhrs)*tips
tipsc= (c/totalhrs)*tips
print('Worker one earned $',tipsa)
print('Worker two earned $',tipsb)
print('Worker three earned $',tipsc)
elif workers == 4:
a=int(input('How many hours did worker 1 work? '))
b=int(input('How many hours did worker 2 work? '))
c=int(input('How many hours did worker 3 work? '))
d=int(input('How many hours did worker 4 work? '))
totalhrs=a+b+c+d
tipsa= (a/totalhrs)* tips
tipsb= (b/totalhrs)*tips
tipsc= (c/totalhrs)*tips
tipsd= (d/totalhrs)*tips
print('Worker one earned $',tipsa)
print('Worker two earned $',tipsb)
print('Worker three earned $',tipsc)
print('Worker four earned $', tipsd)
elif workers > 4:
print('I am not programmed to calculate tips beyond 4 workers, I am a work in progress!')
cartips=tips/cars
print('Our tips per car average today was $ ',cartips)
file.close()
print('Thank you for your help',name,'I hope we work together again soon! :) ')
您好,感谢您花时间帮助我。我是一个业余的、自学成才的 python 编码员。我正在尝试弄清楚如何编写带有条件循环和各种用户输入的 .txt 文件。我意识到这是菜鸟,但如何才能将所有这些信息存储在一个 .txt 文件中呢?
这是我的 valet 停车工作,只是一个有趣的副项目,可以帮助我获得更多经验!谢谢你。也欢迎任何建议,以改进我的代码——我意识到它可能会尖叫菜鸟!
file.write
而不是 print
会将 if 写入文件。 input
也写入监视器而不是文件。检查 https://docs.python.org/3/tutorial/inputoutput.html
你不需要为每个工人重复代码,相反你可以做这样的事情 loops variable sized workers
:
tips = 100
workers = [("w1", 8), ("w2", 8)]
hours = 0
with open("scratch.log", "w") as f:
for worker, hour in workers:
hours = hours + hour
print(hours)
f.write("%s\n" % str(hours))
for worker, hour in workers:
tip = (float(tips) / float(hours)) * hour
print(worker, tip)
f.write("%s %s\n" % (worker, str(tip)))
整体代码为:
newline = '\n'
name = str(input('Name? '))
tips = int(input('Total Tips? '))
workers = int(input('Total Workers Today? '))
cars = int(input('Cars Parked Today? '))
worker_hours = []
total_hours = 0
for idx, worker in enumerate(range(workers)):
count = idx + 1
hours = int(input('How many hours did worker %s work? ' % count))
worker_hours.append(('w' + str(count), hours))
with open("tips.log", "w") as f:
for worker, hours in worker_hours:
total_hours = total_hours + hours
print(total_hours)
f.write("%s" % str(total_hours))
f.write(newline)
for worker, hours in worker_hours:
tip = (float(tips) / float(total_hours)) * hours
print(worker, tip)
f.write("%s %s" % (worker, str(tip)))
f.write(newline)
cartips = tips/cars
print('Tips per car today was $%s.' % cartips)
f.write('Tips per car today was $%s.' % cartips)
f.write(newline)