Posted on:

20 Sept 2024

1

Incorrect output based on usual input in conditional loop program

Hi, I am creating a basic program to find the number of optimal pizzas to be served with minimum extra slices remaining. Here, 3 inputs are number of consumers n, slices consumed per person which is take constant as c, and slices served per pizza offered as s. The output is to display the optimal number of pizzas with least extra slices which of course should be less than s (s slices means 1 pizza). A usual output for say 10 people eating 4 slice person and a pizza has 5 slices will have 8 pizzas with 0 slices left, or 10 people eating 4 slices of pizza with 9 slice worth of pizza should be 5 pizzas with 5 slices left (9*5 - (10*4)). Now, I randomly input 200 people (n), 4 slices per person (c), and 7 slices served per pizza (s). Now the output should return 115 pizzas (115*7 = 805) with 5 slices remaining. But its showing 118 pizzas with 26 slices remaining?? I even asked chatgpt but it suggested me to use math.ceil. Can anyone please tell how to optimize my current code for the desired output without using external packages since its not that complex of a problem. (In the code, n represent number of people, c represents number of slices consumed per person, and s represents slices served in 1 pizza. The logic I am applying is to check if the total slices consumed coincides with absolute multiple of s. if not, then add 1 till s until the total reaches slices divisible completely by s and extra slices is K-T)
 def optimal_pizzas(n,c,s):
    T = n*c 
    i=0
    K=T
    while (i <= s):
        K += i
        if K % s == 0:
            break
        i += 1
    l = K-T
    F = int(K/s)
    print(f"The total number of optimal pizzas is {F} with {l} remaining slices")
    
    
    
n = int(input("Enter number of people eating: "))
c = int(input("Enter number of slices consumed per person: "))
s = int(input("Enter slices on a pizza served"))
optimal_pizzas(n,c,s)
 
0 answers ( 0 marked as helpful)

Submit an answer