如何让一个比萨程序在PYTHON中变成一个完整的比萨饼

2024-10-02 08:16:59 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我用python3.3制作了一个pizza程序,它接受用户的输入并打印所需的比萨数量。每个比萨饼都切成8片。所以它的工作原理是这样的:你输入参加聚会的人数,以及每个人平均吃多少块。然后电脑会告诉你需要点多少批萨。我的问题是,假设有10个人来参加聚会,他们平均每人吃2块。计算机给我2.5,我需要找到一种方法,使它四舍五入到最接近的整数。明白我的意思了吗?这是我目前为止的代码

 eaters = input("How many people are attending the party?")

 pieces = input("How many pieces will everyone eat on average?")

 pizzas = float(eaters) * float(pieces)
 orders_needed = pizzas/8
 print(orders_needed)

有什么办法吗?在


Tags: 用户程序input数量floatmanyhoworders
1条回答
网友
1楼 · 发布于 2024-10-02 08:16:59

math.ceil向上取整。在

import math

eaters = input("How many people are attending the party?")

pieces = input("How many pieces will everyone eat on average?")

pizzas = float(eaters) * float(pieces)
orders_needed = math.ceil(pizzas/8)
print(orders_needed)

相关问题 更多 >

    热门问题