Python Forum
Homework that displays different weights
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework that displays different weights
#1
[
After about an hour of being dumb i finally got the loop to work but now i have a different problem. The program asks for some input like the low end number the high end number and the step number. So when you put in 1,10,1 it'll print out 10 numbers and it works fine. Unfortunately if you put in 1,100,10 (Like in the second example in my lab booklet) it prints out 11 numbers. The numbers are 1,11,21,31,41,51,61,71,81,91 and for some odd reason 92.
Here's the code.
a='Pounds'
b='Ounces'
c='Kilograms'

low=int(input('Enter a low value:'))
high=int(input('Enter a high value:'))
step=int(input('Enter a step value:'))

print('{0:<10}{1:^10}{2:>10}'.format(a,b,c))

for weight in range(low,high,step):
    print('{0:<10.1f}{1:^10.1f}{2:>10.1f}'.format(weight,weight*16,weight*453.592))
else:
    print('{0:<10.1f}{1:^10.1f}{2:>10.1f}'.format(weight+1,(weight+1)*16,(weight+1)*453.592))
Any hint at what is causing this issue would be great.
Reply
#2
drop the else and code after, then add 1 to high .. so you get:
a = 'Pounds'
b = 'Ounces'
c = 'Kilograms'

low = int(input('Enter a low value:'))
high = int(input('Enter a high value:'))
step = int(input('Enter a step value:'))

print('{0:<10}{1:^10}{2:>10}'.format(a, b, c))

for weight in range(low, high+1, step):
    print('{0:<10.1f}{1:^10.1f}{2:>10.1f}'.format(weight, weight * 16, weight * 453.592))
test:
Output:
Enter a low value:1 Enter a high value:10 Enter a step value:1 Pounds      Ounces   Kilograms 1.0          16.0        453.6 2.0          32.0        907.2 3.0          48.0       1360.8 4.0          64.0       1814.4 5.0          80.0       2268.0 6.0          96.0       2721.6 7.0         112.0       3175.1 8.0         128.0       3628.7 9.0         144.0       4082.3 10.0        160.0       4535.9 Enter a low value:1 Enter a high value:100 Enter a step value:10 Pounds      Ounces   Kilograms 1.0          16.0        453.6 11.0        176.0       4989.5 21.0        336.0       9525.4 31.0        496.0      14061.4 41.0        656.0      18597.3 51.0        816.0      23133.2 61.0        976.0      27669.1 71.0        1136.0     32205.0 81.0        1296.0     36741.0 91.0        1456.0     41276.9
Reply
#3
Thank you very much that worked perfectly. Another quick question. The second part of the lab wants multiple inputs within the for loop, but when i coded it(and it's probably me missing something super obvious) it doesn't save all the inputs it only saves the last input.

days=int(input('How many days do you have?'))

for total in range(0,days,1):
    total=int(input('Enter the maximum wind speed in km/hour for day {0}:'.format(total+1)))

print('Minimum speed: {0:.1f}km/h'.format)
print('Maximum speed: {0:.1f}km/h'.format)
print('Average speed: {0:.1f}km/h'.format(total/days))
This is what i have now so when you enter the number of days it'll ask you to input a number for each day but it only stores the last input which makes it so i can't get the average.

Disregard my last post i figured it out. Thank you again for your help with my first problem!
Reply
#4
You have two inputs, one for days, and one for total
the two print statements:
print('Minimum speed: {0:.1f}km/h'.format)
print('Maximum speed: {0:.1f}km/h'.format)
don't have any values after the format,
the syntax is:
print('text: {fieldformat}'.format(value)
Reply
#5
(Oct-20-2017, 09:39 PM)Schmitlab Wrote:
for total in range(0,days,1):
    total=int(input('Enter the maximum wind speed in km/hour for day {0}:'.format(total+1)))
It is not good practice to try to overwrite the iteration variable, which gets overwritten next time around the loop anyway. In this case it is somewhat pointless as well, as you are not retaining or using the assignment to total anyway.

If you want to keep all the inputs obtained in a loop, you need something like a list. Set it to an empty list, e.g. winds = [], before the loop, and then each time through the loop you can append another entry, e.g. winds.append(newwind) where newwind is the value of some input (rather than total).
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  traverses directory recursively and displays files Prani05 7 3,368 Apr-30-2020, 10:25 AM
Last Post: snippsat
  Program that displays the number with the greatest amount of factors ilusmd 3 2,826 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Creating a variable that displays time groovydingo 3 2,914 Apr-17-2018, 04:46 AM
Last Post: tannishpage

Forum Jump:

User Panel Messages

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