如何在python3中链接两条路径?

2024-06-10 23:32:18 发布

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

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

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

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.''')

两次,我只需要写一次。你知道吗


Tags: the代码路径you游戏your链接浪费
3条回答

在这种特殊情况下,您可以为yeet_bleed提供一个默认值。e、 g

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

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

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

使用函数:

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.''')

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

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

What's an alternative to if/elif statements in Python?

相关问题 更多 >