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.
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
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 = []
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[0].
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.
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})