Python选择文本游戏的麻烦(if-then语句不能正常工作)

时间:2013-03-01 17:33:14

标签: python if-statement

我正在尝试在Python 3.2.3中创建一个非常简单的游戏,我从我创建的选项中选择一个字符串字符并返回一个结果。获得结果后,它将循环回主菜单功能以进行另一个选择,或者一个选项将结束程序。

当我创建这个游戏时,我可以在输入时弹出一个选项,但它总是显示前两个选项,而不是我输入的选项。

我的选择结果是加法运算符和打印文本的混合。我不知道这是否有帮助,但我会粘贴下面的代码。

任何人都可以告诉我我做错了什么吗?我认为这是我的if-then-else语句,因为无论我在该函数中移动什么,它总是显示相同的输出。但任何提示都会有所帮助。


编辑:

如果我输入整个代码可能是最好的,因为我现在正在获取

  

NameError:未定义全局名称'xString'

另外:set_sword(),set_rock()也都设置为函数。当用户键入“sword”时应该调用它们,它应该调用get_sword函数(它会执行,但它也会调用set_magic()函数。

以下是编辑过的代码:

# ADDITIONAL DETAILS

# This code calculates the damage you would do if you were a heroic knight that is attacking an evil dragon.
# You have 6 options available in the main menu: Attack with your sword, attack with magic, block with your shield, Throw a rock at it, Run away, and Quit fight (or exit).
# The calculations involve subtracting the attack damage numbers against the dragons HP (or Health Points). For example, if your attack with sword number subtracts the dragons HP number and reaches 0: the dragon will be defeated and the game is “won’.
# When the game is won the game is reset back to the main menu (in this case the main menu are the fighting options.)
# I hope you find this particular project entry unique and fun!
# Main menu function. It should present the options available to input, allow the input of the listed options and be looped until the user uses the quit command.
def set_main():
        sword = set_sword()
        magic = set_magic()
        block = set_block()
        rock = set_rock()
        run = set_run()
        done = set_finish()
        print("Main Menu: ")
        print("Only one of your attacks can lower his HP to zero! What will you do?! ")
        print("Type “sword” to use a sword attack with an attack power of 60! ")
        print("Type “magic” to use a magic attack with an attack power of 80!")
        print("Type “block” to block with your shield with an attack power of 70! ")
        print("Type “rock” to throw a rock! with an attack power of ??? ")
        print("Type “run” to RUN AWAY DUDE! It has an attack power of only 1 though. ")
        print("Type “done” to finish the game and program. ")

# sword attack function that should be called if the input is "sword"
def set_sword():
        sword = str(xString)
        sword = 60
        dragon = 100
        print('The sum of ', sword, ' and ', dragon, ' is ', sword-dragon, ' Attack power! ', sep='')
        print("Your Super Special Overlasting Justice Power Sword attack did little damage!")
        print("The dragon grabs you and eats you whole! Gross.")
        print("Try Again!")
        return set_sword

# magic attack function that should be called if the input is "magic"
def set_magic():
        magic = str(xString)
        magic = 80
        dragon = 100
        print('The sum of ', magic, ' and ', dragon, ' is ', magic-dragon, ' Attack power! ', sep='')
        print("Your magic attack is too weak!")
        print("The dragon uses it's mighty feet and stomps on you!")
        print("So yeah, you're dead. Try again!")
        return set_magic

# blocking attack function that should be called if the input is "block"
def set_block():
        block = str(xString)
        block = 100
        dragon = 100
        print('The sum of ', block, ' and ', dragon, ' is ', block-dragon, ' Attack power! ', sep='')
        print("You blocked the dragon's attack perfectly!")
        print("Both you and the dragon are exhasuted and decide to fight another day!")
        print("So uh...Try again tomorrow?")
        return set_block

# rock throw attack function that should be called if the input is "rock"
def set_rock():
        rock = str(xString)
        rock = 150
        dragon = 100
        print('The sum of ', rock, ' and ', dragon, ' is ', rock-dragon, ' Attack power! ', sep='')
        print("In complete desperation you find a rock next to you and throw it at the dragon!")
        print("The rock hits the drgon square in the eye! It roars in pain!")
        print("The dragon then begins to cry and it doesn't like things hitting his eye.")
        print("The dragon then flies away from the castle in fear!")
        print("So..YOU DID IT! Congratulations! Try one of the other options!")
        return set_rock

# run command function that should be called if input is "run"
def set_run():
        run = str(xString)
        run = 150
        dragon = 100
        print('The sum of ', run, ' and ', dragon, ' is ', run-dragon, ' Attack power! ', sep='')
        print("You decide that saving the world isn't worth it and you run!")
        print("You decide to retire and leave a peaceful life. You find a nice partner, fall in love, and have children.")
        print("several years later the dragon storms into your village and wipes out everything!")
        print("Including you...")
        print("Was your time of peace worth it? Find out by trying again!")
        return set_run

# quit function that should quit the program if the user inputs "done"
def set_finish():
        print("Game over! Thanks for playing!")
        quit
        return set_finish

# default introduction print should explain the game and pretends an ending input.
print("The hero arrives in the dark castle and is welcomed by a large and evil dragon! You are that hero and must defeat the dragon to save the princess!")
print("The Dragon has 100 HP! ")
xString = str(input("What attack will you do?! (Type the attack name to pick an attack) :"))
if xString == "sword":
        print(set_sword())
elif xString == "magic":
        print(set_magic())
elif xString == "block":
        print(set_block())
elif xString == "rock":
        print(set_rock())
elif xString == "run":
        print(set_run())
elif xString == "done":
        print(set_finish())
else:
        print("Pick an option please.")
        quit
print(set_main())
print("Hope you had fun!")

2 个答案:

答案 0 :(得分:1)

你遇到了一些问题,其中最重要的是你要比较变量和字符串。引号计数。而且我认为你还没有理解道文。输入语句后的任何一个语句都不需要在那里:

xString = input("What attack will you do?! (Type the attack name to pick an attack) :")
# xString = 0
# xString = str(xString)

如果您想确保您的输入是字符串,请执行以下操作:

xString = str(input("What attack will you do?! (Type the attack name to pick an attack) :"))
# xString = 0
# xString = str(xString)

在你的if ... elif中,你可以在不需要的时候定义变量。

# sword = set_sword()
# magic = set_magic()
# block = set_block()
# rock = set_rock()
# run = set_run()
# done = set_finish()

if xString == "sword":
        xString = 60 # I hope you know why you changed the value of xString
        print(set_sword())

elif xString == "magic":
        print(set_magic())

elif xString == "block":
        print(set_block())

elif xString == "rock":
        print(set_rock())

elif xString == "run":
        print(set_run())

elif xString == "done":
        print(set_finish())

else:
        print("Pick an option please.")
        quit

答案 1 :(得分:0)

xString = input("What attack will you do?! (Type the attack name to pick an attack) :")
xString = 0
xString = str(xString)

执行这3个语句后xString的值总是'0'。我不明白为什么你有第二和第三个任务 - 你应该删除那些。

修改

# This line reads the user input, which is probably what you want.
xString = input("What attack will you do?! (Type the attack name to pick an attack) :")
# This line throws away the user input in xString by assigning 0 instead
xString = 0
# Since xString = 0, this is the same as xString = str(0), which returns '0'
xString = str(xString)

更新

我之前假设sword = set_sword()正在返回一个字符串,但是现在您已经发布了完整的代码,看起来它正在返回一个函数句柄。您应该遵循James的建议,以便将输入与字符串而不是函数进行比较。代码还有很多其他问题(比如你在quit做了什么?),所以你还有其他一些问题要解决。