如何在字符串中使用函数的结果?

2024-04-19 07:52:06 发布

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

就像定义了一个函数并让它运行一样,它会返回一个结果,但是如何在字符串中使用这个结果呢?因为现在我有一段代码

def chosen_snack():
    while True:
        print "What's your favourite type of snack?"
        fav_type = raw_input()
        if "sweet" in fav_type:
            print "So you like sweet snacks? I'll give you a lollipop!"
        elif "savoury" in fav_type:
            print "So you like savoury snacks? I'll give you some crisps!"
        else:
            print "sorry, what did you say? I only have savoury and sweet snacks."
            continue
        return

chosen_snack()

print "What will you do, eat it or save it?"
choice = raw_input("> ")

if "eat it" in choice:
    print "Well I  also would eat %s" % chosen_snack()

就像我想让所选的零食(比如一些薯片或棒棒糖)被%s“串起来”(我说得通吗?)但我知道怎么做


Tags: inyouinputrawtypeitwhatprint