Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with homework
#1
hey guys, needing some help here! I need to make a program that is going to take a range of numbers which are to be temperatures and store them in a list, it then needs to find the percentage of the numbers in the list that are above 16 can anyone help with this at all? i have put the code below but I cannot for the life of me seem to get it going, im rubbish at programming, I have the bit where it prompts the user for the temperatures and all the temperatures are included that are in the assignment

# work out the percentage of the day that the office was warm enough

#input temperatures of the office on hourly basis
temperatures = [20,15,17,16,14,19,18,19]
for n in range(1, 9):
    temperatures.append(int(input(f'Enter temp {n}: ')))


# find the percentage of the day that the temperature was within acceptable range

# Input a list of 8 numbers
temperatures = [20,15,17,16,14,19,18,19]

#sub-problem: compute the percentage
total = 0
for temperatures in temperatures:
    total = total + temperatures

# sub- problem: compute the length of the list
length = 0
for temperature in temperatures:
    length = length +1

#output: percentage office was warm enough, a number
percentage = total / 100

print('the percentage the office was warm enough', temperatures, 'is', percentage)
Reply
#2
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

There are a few problems with your code. First, line 12 is overwriting all the temperatures you got from the user on lines 5-6. You should get rid of that.

Then look at you loop for computing the percentage, and compare it to the one for the length of the list. The thing is that in for x in y:, x and y need to be two different things. So drop the s on the first temperatures on line 16 and the one on line 17.

That will make it work, but it gives the wrong answer. You're calculating something between an average and a percentage. What you want is the percentage greater than 16. So you need the number of temperatures greater than 16, which is what you should be calculating on lines 16-17. It will be a lot like lines 21-22, but will only add one if the temperature is greater than 16 (if temperature > 16:). Then divide that by length.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
first you create a list of temperatures:
temperatures = [20,15,17,16,14,19,18,19]

for n in range(1, 9):
    temperatures.append(int(input(f'Enter temp {n}: ')))
and then the next thing you do is overwrite the list (line 12):
temperatures = [20,15,17,16,14,19,18,19]
so now everything that you entered is gone!

next the following code (lines 15 - 17):
total = 0
for temperatures in temperatures:
    total = total + temperatures
can be shortened to:
total = sum(temperatures)
and lines (20 - 22) can be shortened to:
length = len(temperature)
now you need to write code to pull all items above 16
and calculate % on result
Reply
#4
oops --- human race condition!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020