Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

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:").
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. My program got the percentage of 37% males instead of 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.

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 )) + '%')
question from:https://stackoverflow.com/questions/65865880/how-do-i-round-to-a-whole-number-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, as you have guessed you are trying to round up a number which is already an integer.

for example: int(10.6) returns the truncated integer part of the number '10.6'. which returns 10in this case.

so if we use round(int(10.6)) what happens is, we're just trying to round up 10. which is similar to round(10).

instead what you wanna do is round(10.6) which returns an 11.

additionally, there are some extra parentheses used in line9 which causes the type error.

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)) + '%')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...