简体   繁体   English

如何在 Python 中舍入为整数?

[英]How do I round to a whole number in Python?

I have an assignment and am having a bit of trouble with it.我有一个任务,我遇到了一些麻烦。

Here is my question:这是我的问题:

Write a program that asks the user for the number of males and the number of females registered in a class using two separate inputs ("Enter number of males:", "Enter number of females:").编写一个程序,使用两个单独的输入(“输入男性人数:”、“输入女性人数:”)询问用户在 class 中注册的男性人数和女性人数。
The program should display the percentage of males and females (round to the nearest whole number) in the following format:程序应按以下格式显示男性和女性的百分比(四舍五入到最接近的整数):

Percent males: 35%
Percent females: 65%

Use string formatting.使用字符串格式。

My issue is that I am not able to round the percent of males and females.我的问题是我无法计算男性和女性的百分比。 For example, the program I am using to grade the code input had 150 males and 250 females.例如,我用来对代码输入进行评分的程序有 150 名男性和 250 名女性。 My program got the percentage of 37% males instead of 38%.我的计划获得了 37% 的男性比例,而不是 38%。

Here is my code that I have now这是我现在拥有的代码

number_of_males = int(input("Enter number of males:"))
number_of_females = int(input("Enter number of females:"))
    
total = (number_of_males + number_of_females)
    
percentage_male = ((number_of_males/total)*100)
percentage_female = ((number_of_females/total)*100)
    
print("Percent males: " + str( int(percentage_male )) + '%')   
print("Percent females: " + str( int(percentage_female )) + '%')

So far I have tried this, but it is giving me an error.到目前为止,我已经尝试过了,但它给了我一个错误。 I am probably typing the round function out incorrectly.我可能打错了圆形 function。

line 9, in <module>
    print("Percent males: " + str( (round(int(percentage_male ))) + '%'))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
number_of_males = int(input("Enter number of males:"))
number_of_females = int(input("Enter number of females:"))
    
total = (number_of_males + number_of_females)
    
percentage_male = ((number_of_males/total)*100)
percentage_female = ((number_of_females/total)*100)
    
print("Percent males: " + str( (round(int(percentage_male ))) + '%')) 
print("Percent females: " + str( int(percentage_female )) + '%')

Yes, as you have guessed you are trying to round up a number which is already an integer.是的,正如您已经猜到的那样,您正在尝试对已经是 integer 的数字进行四舍五入。

for example: int(10.6) returns the truncated integer part of the number '10.6'.例如: int(10.6)返回数字“10.6”的截断 integer 部分。 which returns 10 in this case.在这种情况下返回10

so if we use round(int(10.6)) what happens is, we're just trying to round up 10 .因此,如果我们使用round(int(10.6))会发生什么,我们只是试图四舍五入10 which is similar to round(10) .这类似于round(10)

instead what you wanna do is round(10.6) which returns an 11.相反,您想做的是round(10.6)返回 11。

additionally, there are some extra parentheses used in line9 which causes the type error.此外,line9 中使用了一些额外的括号,导致类型错误。

in your scenario,在你的场景中,

number_of_males = int(input("Enter number of males:"))
number_of_females = int(input("Enter number of females:"))

total = (number_of_males + number_of_females)

percentage_male = ((number_of_males/total)*100)
percentage_female = ((number_of_females/total)*100)

print("Percent males: " + str(round(percentage_male)) + '%')

print("Percent females: " + str(round(percentage_female)) + '%')

will do the job.将完成这项工作。

furthermore, you can rearrange the code by removing unnecessary parentheses.此外,您可以通过删除不必要的括号来重新排列代码。

number_of_males = int(input("Enter number of males:")) 
number_of_females = int(input("Enter number of females:"))

total = number_of_males + number_of_females

percentage_male = number_of_males/total*100 
percentage_female = number_of_females/total*100

print("Percent males: " + str(round(percentage_male)) + '%')
print("Percent females: " + str(round(percentage_female)) + '%')

You can't add a number and the string '%' .您不能添加数字和字符串'%' You can fix it with你可以用

print("Percent males: " + str( (round(int(percentage_male )))) + '%')

But you also don't need the int function.但是您也不需要int function。 It makes no sense to round an integer.舍入 integer 是没有意义的。 int removes the decimal part (round towards zero). int删除小数部分(向零舍入)。 You should skip it:你应该跳过它:

print("Percent males: " + str(round(percentage_male)) + '%')

and get the same result.并得到相同的结果。

number_of_males = int(input("Enter number of males:"))
number_of_females = int(input("Enter number of females:"))

total = number_of_males + number_of_females

percentage_male = number_of_males / total * 100
percentage_female = number_of_females / total * 100

print("Percent males: {}%".format(round(percentage_male)))

print("Percent females: {}%".format(round(percentage_female)))

Python has builtin formatting which include formatting as a presentage, for example: Python 具有内置格式,包括格式作为演示文稿,例如:

>>> p = 0.45
>>> format(p, '.0%')
'45%'
>>> f"{p:.0%}"
'45%'

so in your case it would be:所以在你的情况下,它将是:

percentage_male = number_of_males / total
percentage_female = number_of_females / total

print(f"Percent males: {percentage_male:.0%}")

print(f"Percent females: {percentage_female:.0%}")

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

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