复数,if语句python

时间:2014-03-24 01:20:45

标签: python eclipse

这么多问题..目前我的代码是计算成本,成人和儿童的数量。如果用户只输入1个孩子,我希望它打印孩子,而不是孩子。

serviceType = input("Would you like (M)edium or (R)are steak?")
if serviceType == "r":
    rare = float((noChild * rare) * DISCOUNT) + (noAdult * BASIC)
    print("That is " + formatCurrency(rare) + " for rare choice for " + str(noAdult) + " adults and " + str(noChild) + " children. Enjoy!")

输出结果显示 这是$ - 2名成人和1名儿童的罕见选择。 我需要它说“这是$ - 对于1名成人和1名儿童来说是罕见的选择。

我想我需要放一个

if noChild == 1

声明某处,但不确定将其添加到哪里?

我也有相同格式的代码来计算“中等”,所以我不能将句子改为子,否则如果用户输入2就会是2个孩子。我希望我已经正确地解释了自己?

谢谢

5 个答案:

答案 0 :(得分:1)

使用x if ... else y构造:

def children_str(number):
    return "{} {}".format(number, 'child' if number == 1 else 'children')

for i in range(3):
    print children_str(i)

输出:

0 children
1 child
2 children

你可以概括这个功能:

def pl(number, singular, plural=None):
    if plural == None:
        plural = singular + 's'
    return "{} {}".format(number, singular if number == 1 else plural)

print (pl(1, 'child', 'children')) # 1 adult
print (pl(2, 'child', 'children')) # 2 children
print (pl(1, 'adult', 'adults')) # 1 adult
print (pl(2, 'adult')) # 2 adults
print (pl(1, 'adult')) # 1 adult
print (pl(5, 'adult', 'adults')) # 5 adults

答案 1 :(得分:1)

如果您希望复数和单数更通用,您可以考虑使用变形,an python package

然后转换将如下所示:

import inflect

conversion_engine = inflect.engine()

def main():

    for count in range(4):
        print format_plural_singular("child",count)
        print format_plural_singular("adult",count)
        print

def format_plural_singular(noun,quantity):
    return conversion_engine.plural_noun(noun,quantity)

答案 2 :(得分:0)

您可以像这样编写print语句:

print("That is " + formatCurrency(rare) + " for rare choice for " + str(noAdult) +
     (" adult" if noAdult==1 else " adults") + " and " + str(noChild) +
     (" child" if noChild==1 else " children") + ". Enjoy!")

答案 3 :(得分:0)

def pluralize(singular, plural):
    def fn(num):
        return "{} {}".format(num, singular if num==1 else plural)
    return fn

children = pluralize("child",  "children")
adults   = pluralize("adult",  "adults")
seniors  = pluralize("senior", "seniors")

然后

children(0)    # =>  "0 children"
children(1)    # =>  "1 child"
children(2)    # =>  "2 children"

def participants(num_seniors, num_adults, num_children):
    items = [
        (num_seniors,  seniors),
        (num_adults,   adults),
        (num_children, children)
    ]
    items = [fn(n) for n,fn in items if n > 0]

    # join last pair of items with "and"
    items[-2:] = [" and ".join(items[-2:])]
    # join all preceding items with ","
    return ", ".join(items)

participants(1, 2, 1)  # => "1 senior, 2 adults and 1 child"
participants(0, 1, 4)  # => "1 adult and 4 children"

答案 4 :(得分:0)

我来这里是在寻找一个非常基本的解决方案,该解决方案遵循一个简单的规则,如果计数大于1,则添加 s 。您可以使用Python的f字符串形式执行简单的内联if-在f字符串中声明。

print(f"That is {formatCurrency(rare)} for rare choice for {noAdult} 
        adult{'s' if noAdult != 1 else ''} and {noChild}
        child{'ren' if noChild != 1 else ''}. Enjoy!"
)