How can I solve this python challenge? Please help

You told me to write usage[ ] and i wrote it

OHHH. I wrote usage{ } instead of usage[ ]. Alright I fixed it and it actually worked. Thanks a lot to all of you for helping me in this. Very appreciated.

Did you write usage or usage[0] ?
If you are not sure post your current version of the code.

i wrote usage[0]

while True:
    day = input("Enter weekday: ")
    electricity = float(input("Enter electricity usage: ")) 
    usage.append((day, electricity))
    if len(usage) == 7:
        break
usage.sort()
print("The day you will get free electricity is", usage[0])

Here you go

Was there an error reported? If so post that error, in full.
Was there output that is wrong? If so post the output, in full.

No nothing was wrong. Everything was as needed. Thanks a lot.

1 Like

Have you tested this? It doesn’t do what you want:

Enter weekday: mon
Enter electricity usage: 5
Enter weekday: tue
Enter electricity usage: 5
Enter weekday: wen
Enter electricity usage: 6
Enter weekday: thur
Enter electricity usage: 5
Enter weekday: fri
Enter electricity usage: 4
Enter weekday: sat
Enter electricity usage: 1
Enter weekday: sun
Enter electricity usage: 1
The day you will get free electricity is ('fri', 4.0)

usage is being sorted according to the first element in the tuple, day. You want to sort according to the second element, electricity. Just switch the places day and electricity and you will get the correct result:

while True:
    day = input("Enter weekday: ")
    electricity = float(input("Enter electricity usage: ")) 
    usage.append((electricity, day))
    if len(usage) == 7:
        break
usage.sort()
print("The day you will get free electricity is", usage[0])
Enter weekday: mon
Enter electricity usage: 5
Enter weekday: tue
Enter electricity usage: 5
Enter weekday: wen
Enter electricity usage: 6
Enter weekday: thur
Enter electricity usage: 5
Enter weekday: fri
Enter electricity usage: 4
Enter weekday: sat
Enter electricity usage: 1
Enter weekday: sun
Enter electricity usage: 1
The day you will get free electricity is (1.0, 'sat')

it kind of does what i actually want it to

One way to do this, without having to type each day name:

days = (
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
    )
usage = []

print("Enter the units for each day")
for day in days:
    units = int(input(f"{day}: "))
    usage.append(units)
    free_day = days[usage.index(min(usage))]

print(f"Free day {free_day} with {min(usage)} units used.")

This may or may not fit the challenge, but even if the days need to be entered by hand, rather than auto selected, it can be adapted to suit.

1 Like