Roger has switched to a new electric supplier. He will receive free electricity one day a week. He will not pay for the day that he uses the least amount of electricity. Write a program that will:

• Allow the user to input the day of the week and then units of electricity used.

• Compare them until all comparisons are completed.

• Output the day that will be free of charge.

Hi Zaid, and welcome to discuss.python.org!

Which part of the problem do you need help with, specifically? Please post your own attempt, and we can go from there.

1 Like

Hi. I am completely lost and I don’t know where to begin. Will it be possible if you sent me the entire code and I can interpret it as much as I can? Thank you so much Alexander

If you are new to Python, or programming in general, I recommend taking a look at the official Python tutorial.

To get you started on this particular problem, you can use the `input` function to accept user input:

``````day = input("Enter weekday: ")
electricity = float(input("Enter electricity usage: "))  # Convert from string to floating point number.
``````

You can wrap this in a loop, and store the inputs in a list:

``````usage = []
while True:
day = input("Enter weekday: ")
electricity = float(input("Enter electricity usage: "))
usage.append((day, electricity))
``````

Next, you need to stop the loop once the user has input enough data. You can check the length of the list and `break` out of the loop when the length reaches a certain value:

``````usage = []
while True:
day = input("Enter weekday: ")
electricity = float(input("Enter electricity usage: "))
usage.append((day, electricity))
if len(usage) == 7:
break
``````

or you could have the user input a stop condition:

``````usage = []
while True:
day = input("Enter weekday. Enter STOP when finished: ")
if day == "STOP":
break
electricity = float(input("Enter electricity usage: "))
usage.append((day, electricity))
``````

Next, you need to find the smallest value in the list and `print` the corresponding day. I’ll leave that part as an exercise Start by writing code for this bit first

Show us what you have written and what is not working or is confusing.

ok. i will

OK. i will try this As of now, this is all I have

This is what I currently see. There is an error.

Please copy, paste, and format your code for posting, rather than post a picture or captured image of it.

You can use fences of three backticks before and after code to format it, as follows:

`````````
print("Hello, world!")
```
``````

Ok im sorry

``````usage = []
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. As you requested, the formatted and copy pasted code

Great. In the future, please also include the error message, formatted in the same manner.

To access the contents of a list, use `[ ]`, not ( ).

It still won’t quite do what you want, but you’re on the right track. When you sort a list of tuples, the first element in each tuple is used as the sorting key. In this case, the first element is the name of the day, but you want to sort according to the amount of electricity used. So, it would actually be better to store the inputs like this:

``````usage.append((electricity, day))
``````
``````usage = []
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))
``````
``````Enter weekday: 3
Enter electricity usage: 15
Traceback (most recent call last):
File "c:\Users\User\OneDrive - GEMS EDUCATION\Zaid\Computing\Python Coding Challenges\Challenge 34", line 9, in <module>
print("The day you will get free electricity is", usage(0))
TypeError: 'list' object is not callable
``````

I don’t exactly get it. Can you pls write the whole code as it will be easier for me to know where I need to put this. Having the whole code in front of me will make it a lot easier

The problem is with `usage(0)`. That will try to call `usage`. You don’t want that. You want to get the value at index 0. It should be `usage`.

Also, the last 2 lines should not be indented because you want to do them after the loop has finished. If they are indented more than the `while`, they’ll be run each time round the loop, which is not what you want.

Ok ill fix that thank you.

`````` File "c:\Users\User\OneDrive - GEMS EDUCATION\Zaid\Computing\Python Coding Challenges\Challenge 34", line 9
print("The day you will get free electricity is", usage{0})
^
SyntaxError: invalid syntax
``````
``````usage = []
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})
``````

In programming it’s important to pay attention to the details. Look more closely at what I wrote and what you wrote.

Huh. I don’t understand a single thing.