如何在python 3中链接两条路径?

How to link two paths in Python 3?

好吧,在一个问题之后,我的python代码中有两条单独的路径,我需要它们链接在一起,以避免在游戏的其余部分键入两次,浪费空间和代码。image of my code

如你所见,最后四行左右是相同的,但是有两个不同的缩进。我怎样才能使它们都一样呢?所以不用写:

1
2
print('''You carefully wrap the material around your head, wincing in pain everytime your hand bumps the wound.''')
print('''You wonder what the wound was from. It is unimportant however as the bleeding seems to be slowing.''')

两次,我只写一次。


在每个路径之后对公共函数进行函数调用,或者在函数中编写路径代码,一旦函数返回,就执行公共代码块。

尽量不要使用多个和嵌套的if,使代码混淆-使用句柄函数-解决方案建议在下面的链接中:

对于Python中的if/elif语句,还有什么选择?


使用函数:

1
2
3
def rip_and_bandage():
    print('''You carefully wrap the material around your head, wincing in pain everytime your hand bumps the wound.''')
    print('''You wonder what the wound was from. It is unimportant however as the bleeding seems to be slowing.''')


在这种特殊情况下,您可以为yeet_bleed提供默认值。例如

1
2
3
4
yeet_bleed = 0
# Some input to yeet_bleed here
if bleeding_heal == '1' or yeet_bleed == '1':
    print('something')

关于while True循环的另一个建议是:

1
2
while bleeding_heal not in ['1','2','3']:
    bleeding_heal = input("some text:")