繁体   English   中英

Python选择文字游戏出现问题(如果-那么声明无法正常运行)

[英]Trouble with Python choice text game (if-then statement not working right)

我试图在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!")

您遇到了一些问题,其中最重要的是您正在比较变量和字符串。 引号计数。 而且我认为您仍然不了解道文。 输入语句之后的任何赋值语句都不需要在该位置:

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
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()返回一个字符串sword = set_sword() ,但是现在您已经发布了完整的代码,看起来它正在返回一个函数句柄。 您应该遵循James的建议,以便将输入与字符串而不是函数进行比较。 代码还有很多其他问题(例如,您在使用quit做什么?),因此您仍然需要解决其他问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM