1

This is my entire code:

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)

I want to print the last print command vertically so it turns out in the end like so:

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

1 Answer 1

2

An easy way is to have separate print statements for each line in a for loop:

offsets = [15, 20, 25, 18, 20, 22, -14]
for day in range(7):
    print(days[day], demand[day] + offsets[day])
Sign up to request clarification or add additional context in comments.

3 Comments

It's not enough effort to warrant posting a competing answer, but print(f'{days[day]:11}{demand[day] + offsets[day]:3}'), for example, would give neat columns per the OP's request.
File "<ipython-input-44-2628afb4612c>", line 21, in <module> print(days[day], demand[day]+offsets[day]) TypeError: 'int' object is not subscriptable
'int' object is not subscriptable means you've written something like foo[bar] but when it actually runs, foo is an integer number not a list (or a dict or something else that can be addressed in that way). As @Selcuk suggests, the most likely cause is a typo where you're trying to index into the wrong variable, or you have overwritten the variable that contained the list with a value that's just a number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.