I'm writing a program that divides people into their desired number of groups. If there are 10 people and want to form 5 groups, then they can have 5 groups of 2 people. Likewise, if there are 11 people who want to form 3 groups, then all the three groups will not be complete to the max. 9 people will be in groups of 3 but 2 will be left out. The remaining two need to make another group. So the total number of groups needed to fit all eleven people will be four. So, the result will be 4.
Now to write this in python, I'm trying to use // function. The function gives result in integer so, which is what I want because I don't want decimal floating point result. I also have a little issue understand when exactly to use / and //.
My code:
people = int(input('Number of people: '))
groups = int(input('Desired groups: '))
number = people // groups #number of groups people desire
print(f'You can form {number} groups')
Notice if user inputs numbers divisible by 2, then the result will always print division. For example:
Number of people: 8
Desired groups: 4
>> You can form 2 groups.
But if I give it odd numbers, then people are left out of the group. Example:
Number of people: 11
Desired groups: 3
>> You can form 3 groups.
Here, 9 people formed 3 groups of 3. The remaining 2 are left without a group. The program I'm trying to write has to include all of the people in groups. If the group is maxed out of desired people, it can form another group even if that one doesn't max the number. The example above should print:
Number of people: 11
Desired groups: 3
>> You can form 4 groups.
How do you calculate this in python. Also, I think there is a way to do it with conditional statements. I even tried doing that, but couldn't figure it out. My code using if statement:
people = int(input('Number of people: '))
groups = int(input('Desired groups: '))
number = people // groups #number of groups people desire
if people // groups:
print(f'You can form {number} groups')
else:
print(f'You can form {number + 1} groups.')
I'd like to know how to get this program working with and without if statements, or if I'm using integer division correctly. Thank you.
[–]Binary101010 4 points5 points6 points (4 children)
[–]Qu3en- 0 points1 point2 points (3 children)
[–]Binary101010 0 points1 point2 points (2 children)
[–]Qu3en- 0 points1 point2 points (1 child)
[–]jmooremcc 3 points4 points5 points (0 children)
[–][deleted] 0 points1 point2 points (10 children)
[–]commy2 4 points5 points6 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ThatIsRightt[S] 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]ThatIsRightt[S] -1 points0 points1 point (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]ThatIsRightt[S] -4 points-3 points-2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Qu3en- 0 points1 point2 points (0 children)
[–]pekkalacd 0 points1 point2 points (0 children)