1

我正在尝试在 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!")
4

2 回答 2

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
于 2013-03-01T18:48:37.740 回答
0
xString = input("What attack will you do?! (Type the attack name to pick an attack) :")
xString = 0
xString = str(xString)

xString执行这 3 条语句后的值将始终'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?),因此您仍然需要解决一些其他问题。

于 2013-03-01T17:47:56.763 回答