简体   繁体   English

使用round()在Python中取整

[英]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. 我是Python2.7的新手(就像Day 1的新手一样),我正在尝试编写一个简单的Mils至学位转换程序作为学习练习。 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. 例如-如果用户输入6400密耳,我希望程序返回360度(1度= 17.78密耳),而不是359.955度。 My (limited) understanding of the round function is that it accepts a float and level of precision but does not accept variables. 我对Round函数的(有限的)理解是,它接受浮点数和精度级别,但不接受变量。 How do I pass the sum to round()? 如何将总和传递给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
>>> 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 有关round()的更多信息,请参见: 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) . 如果在打印时不希望保留小数位,可以将c替换为int(c)

Also when your say print var1,var2 it automatically puts a space between the two. 同样,当您说出print var1,var2时,它会自动在两者之间放置一个空格。 You might want to try: 您可能要尝试:

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

You would pass the float as a variable. 您将把float作为变量传递。

round(c, 2)

would round c to two decimal places. 将c舍入到小数点后两位。

For Day 1 knowledge, you're understanding the concept of python quite well. 对于第一天的知识,您已经很好地理解了python的概念。 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. 看来您在舍入变量而不更改变量本身,这是导致问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM