簡體   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