1

Python 3.2.3 で非常に単純なゲームを作成しようとしています。ここでは、作成したオプションの選択から文字列文字を入力し、単一の結果を取得します。結果を取得した後、メイン メニュー機能に戻って別の選択を行うか、1 つのオプションでプログラムを終了します。

このゲームを作成したとき、入力すると選択肢がポップアップ表示されましたが、常に最初の 2 つのオプションが表示され、入力したオプションは表示されません。

私の選択結果は、加算演算子と印刷テキストの混合です。参考になるかわかりませんが、以下にコードを貼り付けておきます。

誰が私が間違っているのか教えてもらえますか? その関数内で何を移動しても、常に同じ出力が表示されるので、それは私の 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'。2 番目と 3 番目の割り当てがある理由がわかりません。おそらく、それらを削除する必要があります。

編集:

# 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()は文字列を返すと思っていましたが、完全なコードを投稿したので、関数ハンドルを返しているように見えます。入力を関数ではなく文字列と比較するように、ジェームズの提案に従う必要があります。コードにはまだ他にも多くの問題があります (たとえば、何をしているのquitですか?)。

于 2013-03-01T17:47:56.763 に答える