gpt4 book ai didi

python - 随机发生器不工作

转载 作者:行者123 更新时间:2023-11-30 22:44:13 27 4
gpt4 key购买 nike

所以我正在使用 python 创建一个基于文本的口袋妖怪游戏。然而,一个错误不断出现,敌人在一个回合中进行两次攻击,尽管它们应该是随机的,但每次都是相同的。对布局表示歉意

代码

import math, random
print("-------------POKEMON BROWN-------------")
print("welcome to pokemon brown")
print("choose a pokemon")
print("Charmander, Squirtle, Bulbasaur")
pokemon_1 = input()
pokemon_1 = (pokemon_1.lower())

print("ahh " + pokemon_1 + " a good choice")

enemy = 1

print("Ok lets battle!")

battle = 1
hp = 20
mp = 10
enemyhp = 20
enemymp = 10
print("----Battle Start----")
print("")
battle = 1
while True:
turn = 1
player_turn = 1
if(pokemon_1 == "squirtle"):
player_turn = 1
print("----Your turn----")
print("")
print("Pokemon:" + pokemon_1 + "/HP = " + str(hp) + "/MP = " + str(mp))
print("Tackle(1 DMG), Tail Whip(2 DMG, 2MP), Water Gun(5 DMG, 10 MP), Back")
move = input()
move = (move.lower())
if(move == "tackle"):
print("Your Squirtle uses tackle and deals 1 DMG")
enemyhp -= 1
turn = 1
if(move == "tail whip"):
if(mp >= 2):
print("Your Squirtle uses tail whip and deals 2 DMG")
enemyhp -= 2
mp -= 2
turn = 1
else:
print("not enough MP")

if (move == "water gun"):
if(mp >= 10):
print("Your Squirtle uses water gun and deals 5 DMG")
enemyhp -= 5
mp -= 10
turn = 1
else:
print("not enough MP")

if(pokemon_1 == "charmander"):
player_turn = 1
print("")
print("----Your turn----")
print("Pokemon: " + pokemon_1 + "/HP = " + str(hp) + "/MP = " + str(mp))
print("Scratch(2 DMG), Ember(4 DMG, 5MP), Back")
move = input()
move = (move.lower())
if(move == "scratch"):
print("Your Charmander uses scratch and deals 2 DMG")
enemyhp -= 2
turn = 1
if(move == "ember"):
if(mp >= 2):
print("Your Charmander uses Ember and deals 4 DMG")
enemyhp -= 4
mp -= 5
turn = 1
else:
print("not enough MP")

if(pokemon_1 == "bulbasaur"):
player_turn = 1
print("")
print("----Your turn----")

print("Pokemon: " + pokemon_1 + "/HP = " + str(hp) + "/MP = " + str(mp))
print("Tackle(1 DMG), Vine whip(5 DMG, 5MP)")
move = input()
move = (move.lower())
if(move == "scratch"):
print("Your Bulbasuar uses Tackle and deals 2 DMG")
enemyhp -= 2
turn = 1
if(move == "tail whip"):
if(mp >= 2):
print("Your Bulbasaur uses Vine Whip and deals 5 DMG")
enemyhp -= 4
mp -= 5
turn = 1
else:
print("not enough MP")

if(turn == 1):
if(enemy == 1):
print("")
print("----Enemy's turn----")
print("Enemy Pokemon: Squirtle / HP = " + str(enemyhp) + " / MP = " + str(enemymp))


if(enemymp == 10):
enemy_move = random.randint(1,6)

if(enemymp < 10 and enemymp >= 2):
enemy_move = random.randint(1,5)

if(enemymp < 2):
enemy_move = 0

if(enemy_move == 1 or 2 or 3):
print("Enemy Squirtle uses tackle and deals 1 DMG")
hp -= 1
turn = 0
if(enemy_move == 4 or 5):
if(mp >= 2):
print("Enemy Squirtle uses tail whip and deals 2 DMG")
hp -= 2
enemymp -= 2
turn = 0
else:
print("not enough MP")

if (enemy_move == 6):
if(mp >= 10):
print("Enemy Squirtle uses water gun and deals 5 DMG")
hp -= 5
enemymp -= 10
turn = 0
else:
print("not enough MP")

外壳

-------------POKEMON BROWN-------------
welcome to pokemon brown
choose a pokemon
Charmander, Squirtle, Bulbasaur
charmander
ahh charmander a good choice
Ok lets battle!
----Battle Start----


----Your turn----
Pokemon: charmander/HP = 20/MP = 10
Scratch(2 DMG), Ember(4 DMG, 5MP), Back
scratch
Your Charmander uses scratch and deals 2 DMG

----Enemy's turn----
Enemy Pokemon: Squirtle / HP = 18 / MP = 10
Enemy Squirtle uses tackle and deals 1 DMG
Enemy Squirtle uses tail whip and deals 2 DMG

----Your turn----
Pokemon: charmander/HP = 17/MP = 10
Scratch(2 DMG), Ember(4 DMG, 5MP), Back

最佳答案

主要问题是您没有正确使用

if(enemy_move == 1 or 2 or 3):
if(enemy_move == 4 or 5):

每次都会评估为True。这是因为逻辑上 0 的 bool 值为 False,任何其他数字的值为 True

>>> bool(0)
False
>>> bool(1)
True

所以你的情况最终是这样的。

if (enemy_move == 1 or True or True):

要正确工作,您需要比较每个值。

if(enemy_move == 1 or enemy_move == 2 or enemy_move == 3)

这可以通过多种方式进行清理,例如。

if enemy_move in (1, 2, 3):
if 1 <= enemy_move <= 3:

现在,您的其他问题包括拥有多个 if 语句,除非您想每次使用 if/elif 时都检查每个语句

if enemy_move in (1, 2, 3):
...
elif enemy_move in (4, 5):
...
elif enemy_move == 6:
...
else:
...

然后,这将按从第一个到最后一个的顺序检查每个,如果一个评估为 False,那么它将继续到下一个,一旦满足条件,True >。然后它不会做剩下的事情。

关于python - 随机发生器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41591210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com