Betastate
Betastate

Reputation: 67

Rounding a sum in Python using round()

I am very new to Python2.7 (like Day 1 new), and I am trying to write a simple Mils to Degrees conversion program as a learning exercise. The program asks the user to choose to convert from degrees to mils or vice versa, then asks for a value. It divides or multiplies accordingly and prints the converted answer. Here's where the problem arises. The converted answer should not be returned as an exact floating point number. For instance- if the user inputs 6400 mils I want the program to return 360 degrees (1 degree = 17.78 mils), not 359.955 degrees. My (limited) understanding of the round function is that it accepts a float and level of precision but does not accept variables. How do I pass the sum to round()?

Your input is greatly appreciated.

import sys
import math

def menu():

    print ""
    print " Mils / Degrees Conversion Calculator"
    print "-" * 38
    print ""
    print "Options: "
    print "1. Degrees to Mils"
    print ""
    print "2. Mils to Degrees"
    print ""
    print "3. Quit"
    print "-" * 20
    print""
    return input ("Choose your option: ")
    print ""

#This function contains my attempt at rounding the sum and returns errors
def m2d(a):
    print "Enter azimuth in mils (ex. 6400)"
    b = 17.78
    c = a / b
    print a, " mils = ", c, "degrees"
    round(c[])

#This function works as intended but does not include round()
def d2m(b):
    print "Enter azimuth in degrees (ex. 90)"
    a = 17.78
    print b, " degrees = ", b * a, "mils"


loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == 1:
       d2m(input("Degrees: "))

    elif choice == 2:
        m2d(input("Mils: "))

    elif choice == 3:
        loop = 0

Upvotes: 0

Views: 6226

Answers (3)

Daniel
Daniel

Reputation: 19547

>>> round(359.955)
360.0

def m2d(a):
    print "Enter azimuth in mils (ex. 6400)"
    b = 17.78
    c = round((a / b),0) #This would round the value to zero decimal places.
    print a, " mils = ", c, "degrees"

>>> m2d(6400)
Enter azimuth in mils (ex. 6400)
6400  mils =  360.0 degrees

For more information on round() see: http://docs.python.org/2/library/functions.html#round

If you want no decimal places when you print you can place replace c with int(c).

Also when your say print var1,var2 it automatically puts a space between the two. You might want to try:

    print a, "mils =", c, "degrees"

Upvotes: 2

Hairr
Hairr

Reputation: 1108

For Day 1 knowledge, you're understanding the concept of python quite well. Now, there is some things in the code that could be changed to make some inputs easier, but it's very simple to modify:

c = a / b
c = round(c)

As it seems you're rounding the variable without changing the variable itself, that what's causing the problem.

Upvotes: 0

Nick Perkins
Nick Perkins

Reputation: 1327

You would pass the float as a variable.

round(c, 2)

would round c to two decimal places.

Upvotes: 1

Related Questions