Write this in string formatting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write this in string formatting

How to 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%

10th Sep 2018, 2:30 AM
lourdes pinder
lourdes pinder - avatar
6 Answers
+ 6
Here's one formatted for the SoloLearn Code Playground (the input would be formatted slightly different for another IDE). Don't forget that for SL, all inputs must be entered at the same time, on separate lines, e.g. type 35, hit enter type 65, hit submit P.s. I hope this isn't just your homework - you will learn to code much better if you try to figure it out yourself and then post what you have done, and then ask for help 😉 https://code.sololearn.com/cWL56C4U28NL/?ref=app
10th Sep 2018, 4:57 AM
David Ashton
David Ashton - avatar
+ 5
lourdes pinder When you ask for help with a specific problem and someone takes the time to write some code to answer your question, it is traditional, if not just good manners, to at least upvote their answer or code, and even, to thank them.
13th Sep 2018, 6:47 AM
David Ashton
David Ashton - avatar
+ 1
Which language? On Java you can use math round method and println
10th Sep 2018, 2:44 AM
Alexander Santos
Alexander Santos - avatar
0
Are you still out there David? I realize this thread is a bit old but Im having issued with this same program. Im using Python, so my code isnt the exact same as yours but my math comes out the same as yours and thats where the problem lies. Did you notice that your percentages for the males and females does not add up to 100 under certain circumstances? Its an issue of rounding and that's where Im failing. Using 150 males and 250 females our math ends up with exactly 37.5% male and 62.5% female. Together that equals 100%. But when those numbers are rounded as requested, you end up with 38% and 63%, getting 101% total. This is where my code "fails" the test. Ive thought about doing an end around for the issue and just getting the percentage of one, then using the difference from 100 for the other, but it feels like thats cheating and missing the point of this lesson.
29th Jun 2020, 4:08 AM
Sean Neiber
Sean Neiber - avatar
0
So much for that. I just noticed that your script is rounding down the 62.5 and rounding up the 37.5. How does that work? I can only get the program to round them both up, or drop the remainder from both, leaving me either 99% or 101% but never 100%. I cant figure it out by looking at your code alone.
29th Jun 2020, 4:13 AM
Sean Neiber
Sean Neiber - avatar
0
I wish I didnt seem like such a newb but I just realized your code IS in Python.. Im my defense though, Ive only been doing this for 4 days now. The way you had all the math and everything on one line made it seem like a different language. I tried it every way I have been taught so far and none of those methods worked. I was converting the float to int or using format '.0f' to round off all the decimals, and since Im so new, I throught the round() function was doing that same thing, but after digging into your code, I found 2 methods to get it working. Yours and function '.0%' seem to round the numbers off in a way that makes them always add up to 100% when the program runs. I dont understand how that math works, I really need to review that. From what I have been thought about rounding my whole life, the method that was making my code not work is how its supposed to be done. I realize that at this point you didnt actually do anything for me in particular, but your answer got me looking at this from the a different angle and that helped a lot, so thank you.
29th Jun 2020, 4:49 AM
Sean Neiber
Sean Neiber - avatar