随机工作不起作用

时间:2017-01-11 12:38:42

标签: python

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

CODE

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

SHELL

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

5 个答案:

答案 0 :(得分:2)

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

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

每次评估为True。这是因为逻辑0的布尔值为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。然后它不会做其余的事。

答案 1 :(得分:1)

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

检查enemy_move是否等于1,如果计算结果为false,则检查2是否为真值,它始终是。要检查值是否是三者中的任何一个,您需要进行三次比较,或者您可以使用in

if enemy_move in (1, 2, 3):

或者您可以使用双面范围检查:

if 1 <= enemy_move < 4:

这是python,所以你不需要在if条件周围添加括号。

答案 2 :(得分:0)

在if语句中

,如:

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

请注意,只有前两个将始终一起执行,因为当if计算时,它检查[如果enemy_move等于1]或者如果[2]或[3],则2和3应该是enemy_move == 2并且enemy_move == 3,如果你没有把敌人移动==&#39;它总是用if运算符计算为TRUE,这就是为什么只有最后一个运算符     ...                 if(enemy_move == 6):     ... 不要一起执行。

答案 3 :(得分:0)

当你使用随机时,你应该总是使用一个唯一的种子,否则生成的数字将始终是相同的

在您启动程序时设置一次种子

CREATE TABLE IF NOT EXISTS `shmarket`.`CITIES` (
`cityId` VARCHAR(6) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`PROVINCES_provinceId` VARCHAR(6) NOT NULL,
 PRIMARY KEY (`cityId`),
 INDEX `fk_CITIES_PROVINCES1_idx` (`PROVINCES_provinceId` ASC),
 CONSTRAINT `fk_CITIES_PROVINCES1`
  FOREIGN KEY (`PROVINCES_provinceId`)
 REFERENCES `shmarket`.`PROVINCES` (`provinceId`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION)
 ENGINE = InnoDB
 DEFAULT CHARACTER SET = utf8
 COLLATE = utf8_unicode_ci;


-----------------------------------------------------
Table `shmarket`.`USERS`
-----------------------------------------------------
 CREATE TABLE IF NOT EXISTS `shmarket`.`USERS` (
 `userId` INT NOT NULL AUTO_INCREMENT,
 `name` VARCHAR(45) NOT NULL,
 `surname` VARCHAR(45) NOT NULL,
 `email` VARCHAR(45) NOT NULL,
 `password` VARCHAR(45) NOT NULL,
 `registryDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`role` VARCHAR(6) NOT NULL,
`CITIES_cityId` VARCHAR(6) NOT NULL,
 PRIMARY KEY (`userId`),
 UNIQUE INDEX `email_UNIQUE` (`email` ASC),
 INDEX `fk_USERS_CITIES1_idx` (`CITIES_cityId` ASC),
 CONSTRAINT `fk_USERS_CITIES1`
  FOREIGN KEY (`CITIES_cityId`)
 REFERENCES `shmarket`.`CITIES` (`cityId`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION)
 ENGINE = InnoDB;

你使用的种子并不重要,只要它变化。大多数人使用的最佳示例是当前时间戳。

random.seed( ... )

希望这会有所帮助

答案 4 :(得分:0)

我认为问题在于if语句 请勿使用if语句执行此操作:

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

您想要做的是以下内容:

if (enemy_move == 2 or enemy_move == 3 or enemy_move =-- 4)