Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists
#1
Hello everyone
I am very close to being able to finish this one. I just have a formatting issue that my professor requires be done in a certain way.
He wants all of the data to be in three columns relatively neat looking. The point of this program is to accept two values (temperatures in this case) n and m. It will take every integer between n and m and calculate it from Fahrenheit (which is what you enter it in as) and calculate it to Celsius and Rankine. So, if n is 1 and m is 5 it will calculate 1, 2, 3, 4, and 5 into Celsius and Rankine.
Each of these must be stored in a separate list which I have done. I asked my professor how to do this and he told me to output all three lists on the same line (so the output lines up and looks good), and he told me to print all three lists on the same line. It works sometimes, but not always. If n is greater than 0, it throws an Index Error. If m is less than 0 it throws the same error. The bounds for this are no less then -30 and no more than 130 for n and m values. However, if you make n = -20 and m = 0, it works just fine and prints what it's supposed to. Also, if you make n too large (ex: n = 70 and m = 100), it will throw the same IndexError saying the list index is out of range.
Let me know if I explained that enough if not, I'll try to go into more detail
my code:
listf = []
listc = []
listk = []
n = int(input("Enter start value: "))
m = int(input("Enter end value: "))

print("Fahr", "\t" "Celsius", "\t" "Rankine")

# first for loop calculates celcius and adds it to listc
for ftc in range(n,(m + 1)):
    c1 = ((5/9) * (ftc - 32))
    listc.append(c1)
# this loop adds each base temp into listf
for ftc in range(n, (m + 1)):
    f1 = ftc
    listf.append(f1)
    # this loop calculates rankine and adds it to listk
for ftc in range(n, (m + 1)):
    k1 = ftc + 459.67
    listk.append(k1)
#the next for loop is going to print everything vertically
for i in listf:
    print(i, "\t", "{:,.2f}".format(listc[i]), "\t", listk[i])
kabeer_m likes this post
Reply
#2
Rather than iterating overlistf, try iterating over the length of you lists like this:
listf = []
listc = []
listk = []
n = int(input("Enter start value: "))
m = int(input("Enter end value: "))
 
print("Fahr", "\t" "Celsius", "\t" "Rankine")
 
# first for loop calculates celcius and adds it to listc
for ftc in range(n,(m + 1)):
	c1 = ((5/9) * (ftc - 32))
	listc.append(c1)
# this loop adds each base temp into listf
for ftc in range(n, (m + 1)):
	f1 = ftc
	listf.append(f1)
	# this loop calculates rankine and adds it to listk
for ftc in range(n, (m + 1)):
	k1 = ftc + 459.67
	listk.append(k1)
#the next for loop is going to print everything vertically
for i in range (len (listf)) :
	print (f'{listf [i]:,.2f}\t{listc [i]:,.2f}\t\t{listk [i]:,.2f}')
Output:
Enter start value: 55 Enter end value: 58 Fahr Celsius Rankine 55.00 12.78 514.67 56.00 13.33 515.67 57.00 13.89 516.67 58.00 14.44 517.67
kabeer_m likes this post
Reply
#3
IndexError is coming from iterating over items in listf. If values are 1...5 then their indices are 0...4 and indice 5 is 'out of bounds'.

There is no need to iterate 3 times. More descriptive names can improve readability. Instead of anti-pattern of using indices (this is Python, not Java) one should iterate directly over items:

fahrenheit = []
celsius = []
rankine = []

n = int(input("Enter start value: "))
m = int(input("Enter end value: "))

print(f"{'Fahrenheit':<12}{'Celsius':<12}{'Rankine':<12}")

for temp in range(n,(m + 1)):
    fahrenheit.append(temp)
    celsius.append((5/9) * (temp -32))
    rankine.append(temp + 459.67)

for fahr, cels, rank in zip(fahrenheit, celsius, rankine):
    print(f"{fahr:<12}{cels:<12,.2f}{rank:<12,.2f}")
Output:
Enter start value: 55 Enter end value: 58 Fahrenheit Celsius Rankine 55 12.78 514.67 56 13.33 515.67 57 13.89 516.67 58 14.44 517.67
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thank you for your input. That's exactly what I needed. As for the iterating 3 times, yeah, I know there are definitely better ways to do it I'm pretty new at python. The advice is most appreciated, however.
Reply


Forum Jump:

User Panel Messages

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