如何使一段代码稍后可调用

时间:2017-05-05 02:16:00

标签: python python-3.x

只是为了参考,这是一个有效的商店。我只是想知道是否有可能让它在另一个时间可以调用。例如

    x = input("If you want to go to shop type 'Yes'")
    if x == "Yes":

我不知道它可能,但我认为它需要一个功能,它会调用那么大的代码块。任何帮助表示赞赏。

    shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300", "Bloodcursed Sword: 160 Damage, Cost 800"]
    shopitemsM = ["Fire Throw: 150 Damage, Cost: 700", "Ice Wind: 120 Damage, Cost: 300", "Electric shock: 160 Damage, Cost 800"]


    print("Welcome to the shop.")
    print('')
    if character == "Fighter":
        g = ', '
        print(g.join(shopitemsF))
        time.sleep(1)
    elif character == "Mage":
        g = ', '
        print(g.join(shopitemsM))
        time.sleep(1)

    shopchoice = input("Please pick another item? ")
    print('')

    found = False
    while found != True:
        for text2 in shopitemsF:
            if shopchoice in text2:
                print(text2)
                if int(text2[-3:]) >= gold:
                    print("You need another", int(text2[-3:]) - gold, "gold.")
                    shopchoice = input("Please pick another item? ")
                    break
                elif int(text2[-3:]) <= gold:
                    print("You have purchased,", text2[:-11]+".")
                    x = (int(text2[-21:-18]))
                    gold = gold - int(text2[-3:])
                    found = True

        for text2 in shopitemsM:
            if shopchoice in text2:
                print(text2)
                if int(text2[-3:]) >= gold:
                    print("You need another", int(text2[-3:]) - gold, "gold.")
                    shopchoice = input("Please pick another item? ")
                    break
                elif int(text2[-3:]) <= gold:
                    print("You have purchased,", text2[:-11]+".")
                    x = (int(text2[-21:-18]))
                    gold = gold - int(text2[-3:])
                    found = True

2 个答案:

答案 0 :(得分:0)

是的,你基本上拥有它。您需要创建一个函数并调用该函数。

x = input("If you want to go to shop type 'Yes'")
if x == "Yes":
    go_to_shop()

def go_to_shop():
    <insert your big block of code here>

确保缩进大块代码,使其显示在“def go_to_shop():”的右侧。 Python使用indention而不是braces {}来包含功能块。

答案 1 :(得分:0)

在这里添加了另一个答案来解决mykill456在上面评论中提出的关于想要访问&#34; x =(int(text2 [-21:-18]))&#34; go_to_shop()函数之外的变量。

def go_to_shop():
    global y

    shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300", "Bloodcursed Sword: 160 Damage, Cost 800"]
    shopitemsM = ["Fire Throw: 150 Damage, Cost: 700", "Ice Wind: 120 Damage, Cost: 300", "Electric shock: 160 Damage, Cost 800"]


    print("Welcome to the shop.")
    print('')
    if character == "Fighter":
        g = ', '
        print(g.join(shopitemsF))
        time.sleep(1)
    elif character == "Mage":
        g = ', '
        print(g.join(shopitemsM))
        time.sleep(1)

    shopchoice = input("Please pick another item? ")
    print('')

    found = False
    while found != True:
        for text2 in shopitemsF:
            if shopchoice in text2:
                print(text2)
                if int(text2[-3:]) >= gold:
                    print("You need another", int(text2[-3:]) - gold, "gold.")
                    shopchoice = input("Please pick another item? ")
                    break
                elif int(text2[-3:]) <= gold:
                    print("You have purchased,", text2[:-11]+".")
                    y = (int(text2[-21:-18]))
                    gold = gold - int(text2[-3:])
                    found = True

        for text2 in shopitemsM:
            if shopchoice in text2:
                print(text2)
                if int(text2[-3:]) >= gold:
                    print("You need another", int(text2[-3:]) - gold, "gold.")
                    shopchoice = input("Please pick another item? ")
                    break
                elif int(text2[-3:]) <= gold:
                    print("You have purchased,", text2[:-11]+".")
                    y = (int(text2[-21:-18]))
                    gold = gold - int(text2[-3:])
                    found = True

y = 0

x = input("If you want to go to shop type 'Yes'")
if x == "Yes":
    go_to_shop()
    print('here is y:', y)