简体   繁体   English

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

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

I am trying to make a very simple game in Python 3.2.3 where I input a string character from a choice of options I have created and get a single result back. 我试图在Python 3.2.3中制作一个非常简单的游戏,在该游戏中,我从创建的选项中输入了字符串字符,并返回了单个结果。 After I get the results, it will will either loop back to the main menu function to make another choice or one option will end the program. 得到结果后,它将循环回到主菜单功能以做出另一选择,或者一个选项将结束程序。

When I created this game, I was able to make a choice pop up when I typed it in but it always shows the first two options and not the one I input. 当我创建该游戏时,我在键入时能够弹出一个选择,但它始终显示前两个选项,而不显示我输入的选项。

My choice results are a mixture of addition operators and print text. 我选择的结果是加法运算符和打印文本的混合。 I don't know if this helps but I will paste the code below. 我不知道这是否有帮助,但是我将在下面粘贴代码。

Can anyone please tell me what I'm doing wrong? 谁能告诉我我在做什么错? I think it's my if-then-else statement since no matter what I move around in that function it always shows the same output. 我认为这是我的if-then-else语句,因为无论我在该函数中四处移动,它始终显示相同的输出。 But any tips would be helpful. 但是任何提示都会有所帮助。


Edit: 编辑:

It may be best if I put in the entire code since I am now getting the 最好输入完整的代码,因为现在我得到了

NameError: global name 'xString' is not defined NameError:全局名称'xString'未定义

Also: the set_sword(), set_rock() are all set as functions as well. 另外:set_sword(),set_rock()也都设置为函数。 They should be called when the user types "sword" and it should call the get_sword function (which it does but then it ALSO calls the set_magic() function. 当用户键入“ sword”时,应调用它们,并且应调用get_sword函数(这样做,但随后还应调用set_magic()函数。

Here is the edited code: 这是编辑后的代码:

# 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!")

You've got some problems, not the least of which is that you're comparing variables and strings. 您遇到了一些问题,其中最重要的是您正在比较变量和字符串。 Quotation marks count. 引号计数。 And I don't think you are still understanding DaoWen. 而且我认为您仍然不了解道文。 Neither of your assignment statements after your input statement need to be there: 输入语句之后的任何赋值语句都不需要在该位置:

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

If you want to assure that your input is a string, do this: 如果要确保您的输入是字符串,请执行以下操作:

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

And in your if...elif, you define variables when you don't have to. 并且在您的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)

The value of xString after executing those 3 statements will always be '0' . 执行这3条语句后, xString的值将始终'0' I don't understand why you have the 2nd and 3rd assignments—you should probably just delete those. 我不明白为什么要分配第二和第三作业-您可能应该删除它们。

Edit: 编辑:

# 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)

Update: 更新:

I was assuming before that sword = set_sword() was returning a string, but now that you've posted your full code it looks like it's returning a function handle. 我之前以sword = set_sword()返回一个字符串sword = set_sword() ,但是现在您已经发布了完整的代码,看起来它正在返回一个函数句柄。 You should follow James's suggestions so that you're comparing the input to strings rather than functions. 您应该遵循James的建议,以便将输入与字符串而不是函数进行比较。 There are still a lot of other issues with the code (eg what are you doing with quit ?), so you'll still have some other issues to work out. 代码还有很多其他问题(例如,您在使用quit做什么?),因此您仍然需要解决其他问题。

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

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