Home > Blockchain >  Can't change whole number to number with 1 decimal point
Can't change whole number to number with 1 decimal point

Time:02-03

This program coverts pounds to ounces and kilograms using minimum value, maximum value, and steps.

import numbers


minVal = 0
maxVal = 0
steps = 0
ounces = 0
kilograms = 0

minVal = int(input("Enter the minimum value: "))
maxVal = int(input("Enter the maximum value: "))
steps = int(input("Enter the value of the steps: "))

print("_" * 35)
print("")
print("Pounds", "Ounces", "Kilograms")

for i in range(minVal, maxVal   1, steps):
    print("\n")
    for z in range(1, 2, steps):
        ounces = i * 16
    for x in range(1, 2, steps):
        kilograms = (i * 0.453592)
        print("M" % i, "m " % ounces,"m " % kilograms, end = "")

For some reason the kilogram part is only printing the value as a whole number. I want to make it only 1 decimal point.

CodePudding user response:

You need to use %.1f to print kilograms to 1 decimal place.

CodePudding user response:

Changed "m " % kilograms to "%.1f " % kilograms:

import numbers

minVal = 0
maxVal = 0
steps = 0
ounces = 0
kilograms = 0

minVal = int(input("Enter the minimum value: "))
maxVal = int(input("Enter the maximum value: "))
steps = int(input("Enter the value of the steps: "))

print("_" * 35)
print("")
print("Pounds", "Ounces", "Kilograms")

for i in range(minVal, maxVal   1, steps):
    print("\n")
    for z in range(1, 2, steps):
        ounces = i * 16
    for x in range(1, 2, steps):
        kilograms = (i * 0.453592)
        print("M" % i, "m " % ounces,"%.1f " % kilograms, end = "")

Output:

Pounds Ounces Kilograms


   1     16  0.5

   3     48  1.4

   5     80  2.3

   7    112  3.2

   9    144  4.1
  •  Tags:  
  • Related