Write a program that calculates the number of cakes needed for a given number of guests at a party, where each guest receives 1 slice of cake. The program should ask the user to enter the number of people that are attending, and the number of slices in each cake. The program will print the number of cakes needed. Note: You will need to import the math module
时间: 2024-01-04 16:02:18 浏览: 76
Sure! Here's a program that calculates the number of cakes needed for a given number of guests at a party:
```python
import math
def calculate_cakes_needed():
num_people = int(input("Enter the number of people attending the party: "))
slices_per_cake = int(input("Enter the number of slices in each cake: "))
cakes_needed = math.ceil(num_people / slices_per_cake)
print("Number of cakes needed:", cakes_needed)
calculate_cakes_needed()
```
In this program, we first import the `math` module to use the `ceil()` function, which rounds up a number to the nearest integer.
Then, we define the `calculate_cakes_needed()` function that asks the user to enter the number of people attending the party and the number of slices in each cake. We convert the user inputs to integers using `int()`.
Next, we calculate the number of cakes needed by dividing the number of people by the number of slices per cake, and rounding up using `math.ceil()`.
Finally, we print the number of cakes needed.
Note that the code assumes that each guest will only have one slice of cake.
阅读全文