gpt4 book ai didi

python - 如何在Python中垂直打印每个变量

转载 作者:行者123 更新时间:2023-12-01 00:17:36 24 4
gpt4 key购买 nike

这是我的整个代码:

days=["Sunday: ", "Monday: ", "Tuesday: ", "Wednesday: ", "Thursday: ", "Friday: ", "Saturday: "]
demand=[]
temp=[]
demand=[15, 20, 25, 18, 20, 22, 14]
while demand[0]>0 or demand[1]>0 or demand[2]>0 or demand[3]>0 or demand[4]>0 or demand[5]>0 or demand[6]>0:
min1=min(demand)
ind1=demand.index(min1)
temp=demand.copy()
temp[ind1]=100000
min2=min(temp)
ind2=temp.index(min2)
print(min1, ind1, min2, ind2)
for i in range(7):
if i != ind1 and i != ind2:
demand[i]-=1
print(demand)
print(days[0], demand[0]+15, days[1], demand[1]+20, days[2], demand[2]+25, days[3], demand[3]+18, days[4], demand[4]+20, days[5], demand[5]+22, days[6], demand[6]-14)

我想垂直打印最后一个 print 命令,所以最终结果如下:

Sunday:     15
Monday: 20
Tuesday: 25
Wednesday: 18
Thursday: 20
Friday: 22
Saturday: 14

最佳答案

一个简单的方法是为 for 循环中的每一行使用单独的 print 语句:

offsets = [15, 20, 25, 18, 20, 22, -14]
for day in range(7):
print(days[day], demand[day] + offsets[day])

关于python - 如何在Python中垂直打印每个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59204693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com