我的函数和if语句似乎不起作用

时间:2018-09-17 11:30:50

标签: python function if-statement

 import time

def temp():
    print(script)
    print(a,sce1)
    print(b,sce2)
    print(c,sce3)
    print(d,sce4)
    choice = str(input())
    return choice
    if choice =="i":
        print(inventory)
    if choice =="p":
        print(storyline_varable)


script = ""
a = ""
b = ""
c = ""
d = ""
sce1 = ""
sce2 = ""
sce3 = ""
sce4 = ""
choice = ""
inventory = list((""))
storyline_variable = list((""))


print("Welcome to choice dungeon! Go through this dangerous place by typing the letter of the choice you'd like to make (also type 'i' to see your inventory and 'p' to see important choices made), good luck!")
time.sleep(4)


#start
script = "You lay shackled to a wall in a room with damp walls made of rough stone, you are cold and wet, what do you do?"
sce1 = "Pull the shackles"
sce2 = "Shout for help"
sce3 = "Sit in the cold dark room for a while"
sce4 = "Start nawing your hand off"
a = "a-"
b = "b-"
c = "c-"
d = "d-"

if temp() == "a" or temp() == "ba" or temp() == "ca":
    #a&ba&ca
    script = "Your shackles break free, there is a dimmly lit room with a skeleton, a torch and a closed door"
    sce1 = "Grab the torch"
    sce2 = "Search the skeleton"
    sce3 = "Go through the door"
    a = "aa-"
    b = "ab-"
    c = "ac-"
    d = "ad-"

if temp() == "b":
    #b&cb&db
    storyline_variable.append("Someone has been woken up")
    script = "You call for hours to no avail, until you hear a faint shout from a distance: 'Shut up'. Try another option"
    sce1 = "Pull the shackles"
    sce2 = "Shout for help...again"
    sce3 = "Sit in the cold dark room for a while"
    sce4 = "Start nawing your hand off"
    a = "ba-"
    b = "bb-"
    c = "bc-"
    d = "bd-"

if temp() == "c" or temp() == "bc" or temp() == "cc":
    #c&cc
    script = "You just sit there, it's a bit uncomfortable and a tad boring but it isn't that bad. Try another option"
    sce1 = "Pull the shackles"
    sce2 = "Shout for help"
    sce3 = "Sit there more"
    sce4 = "Start nawing your hand off"
    a = "ca-"
    b = "cb-"
    c = "cc-"
    d = "cd-"

if temp() == "d" or temp() == "bd" or temp() == "cd":
    print("Was it worth it? You bled out... ouch")

else:
    print("Pressing a random button doesn't help you know? You know what? You died for being stupid")

这是我似乎无法使用的完整代码,有人可以帮忙吗? 我不是100%知道if语句中函数如何工作以及如何使用它们,我将如何解决它? 唯一显示正确消息的是,如果我先输入“ a”,然后输入“ b”,然后输入“ c”,然后输入“ d”,其他任何重复#start的行,我将无法找出原因。 如您所见,我正在制作一款地牢和龙类的游戏,您也许还可以告诉我使用python相对较新,如果无法理解您给出的答案,我感到非常抱歉。另外请记住,此代码旨在通过“地牢”进行,因此可能有些元素对此进行了说明。

2 个答案:

答案 0 :(得分:0)

脚本中的逻辑问题很简单:您可以在函数choice中设置变量temp。您应该在测试之前设置变量。您可以通过将temp的第一次执行放在if...

之前来实现

此外,将参数传递给函数是一个好主意,这样您就可以停止使用全局变量来简化脚本。

def temp():
    print(script)
    print(a,sce1)
    print(b,sce2)
    print(c,sce3)
    choice = str(input())
    return choice

#start
script = "You lay shackled to a wall in a room with damp walls made of rough stone, you are cold and wet, what do you do?"
sce1 = "Pull the shackles"
sce2 = "Shout for help"
sce3 = "Sit in the cold dark room for a while"
sce4 = "Start nawing your hand off"
a = "a-"
b = "b-"
c = "c-"
d = "d-"
choice = temp()
if choice == "a":
    #a
    script = "Your shackles break free, there is a dimmly lit room with a skeleton, a torch and a closed door"
    sce1 = "Grab the torch"
    sce2 = "Search the skeleton"
    sce3 = "Go through the door"
    a = "a-"
    b = "b-"
    c = "c-"
    choice = temp()

答案 1 :(得分:0)

def tmp(n):
    print(story[n]['script'])
    print('a- ' + story[n]['sce1'])
    print('b- ' + story[n]['sce2'])
    print('c- ' + story[n]['sce3'])
    print('d- ' + story[n]['sce4'])

story = [{
             'script': "You lay shackled to a wall in a room with damp walls made of rough stone, you are cold and wet, what do you do?",
             'sce1': "Pull the shackles",
             'sce2': "Shout for help",
             'sce3': "Sit in the cold dark room for a while",
             'sce4': "Start nawing your hand off"
},
{
             'script': "Your shackles break free, there is a dimmly lit room with a skeleton, a torch and a closed door",
             'sce1': "Grab the torch",
             'sce2': "Search the skeleton",
             'sce3': "Go through the door",
             'sce4': "Start nawing your hand off"
},
]
tmp(0)
inp = input()
if inp == 'a':
    tmp(1)
相关问题