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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…