简体   繁体   中英

Input from the user is always viewed as invalid in python

I'm building a turn-based simulator of pokemon. More specifically, the battle between Pikachu and Spearow(with some creative liberties taken). The issue is that whenever I ask the user for what move they want to use, it always give back the response that the move is invalid.

# Import Libraries and Methods
import abc
from random import randint
from enum import Enum

DamageTypes = Enum("DamageTypes", "Damaging")
Types = Enum("Types", "ELECTRIC NORMAL FLYING")

class attack(metaclass=abc.ABCMeta):
    @abc.abstractproperty
    def damage_type(self):
        return NotImplemented

    @abc.abstractproperty
    def move_type(self):
        return NotImplemented

class Tackle(attack):

    #def __bool__(self):
        #return self.Tackle is False

    @property
    def damage_type(self):
        return DamageTypes.Damaging

    @property
    def move_type(self):
        return Types.NORMAL


class Thundershock(attack):

    #def __bool__(self):
        #return self.Thundershock is False

    @property
    def damage_type(self):
        return DamageTypes.Damaging

    @property
    def move_type(self):
        return Types.ELECTRIC

class Peck(attack):

    #def __bool__(self):
        #return self.Peck is False

    @property
    def damage_type(self):
        return DamageTypes.Damaging

    @property
    def move_type(self):
        return Types.FLYING

def damage_calculator(monster1, monster2):
    #Critical Strike Chance Modifier
    Critical = 1
    Critical_Chance = randint(0,9)
    if Critical_Chance == 0:
        Critical = 2

    #STAB (or Same Type Attack Bonus) Modifier
    STAB = 1
    #for player
    if monster1.attack is True:
        if Thundershock(attack) is True:
            STAB = 1.5
        elif Tackle(attack) is True:
            STAB = 1
    #for enemy
    if monster2.attack is True:
        if Peck(attack) is True:
            STAB = 1.5
        if Tackle(attack) is True:
            STAB = 1.5

    #Modifier for Minimum Possible Damage Dealt
    DmgModifierLow = STAB*Critical*0.85
    #Modifier for Maximum Possible Damage Dealt
    DmgModifierHigh = STAB*Critical

    if monster1.attack is True:
        if 'user_choice' == 'thundershock':
            AttackDamage = magic_1
            DamageDefense = magic_defense_2
            Base = 40
       elif 'user_choice' == 'tackle':
            AttackDamage = attack_1
            DamageDefense = defense_2
        Base = 40
    elif monster2.attack is True:
        if wild_choice == 0:
            AttackDamage = attack_2
            DamageDefense = defense_1
            Base = 40
        elif wild_choice == 1:
            AttackDamage = attack_2
            DamageDefense = defense_1
            Base = 35

    DamageHigh = ((210/250)*(AttackDamage/DamageDefense)*    (Base)+2)*DmgModifierHigh
    DamageLow = ((210/250)*AttackDamage/DamageDefense*(Base)+2)*DmgModifierLow
    DamageDealt = randint(DamageLow,DamageHigh)

    if monster1.attack:
       monster2.current_health_2 -= DamageDealt
    if monster2.attack:
        monster1.current_health_1 -= DamageDealt

# create the stats for the monsters
Pikachu = []
Spearow = []

# apply stats to Pikachu
health_1 = randint(180, 211)
#health that is modified
current_health_1 = health_1
attack_1 = randint(115, 146)
magic_1 = randint(85, 116)
defense_1 = randint(105, 136)
magic_defense_1 = randint(105, 136)
speed_1 = randint(185, 216)

# apply stats to Spearow
health_2 = randint(190, 221)
#health that is modified
current_health_2 = health_2
attack_2 = randint(125, 156)
magic_2 = randint(65, 96)
defense_2 = randint(67, 98)
magic_defense_2 = randint(67, 98)
speed_2 = randint(145, 176)

def standby_battle_text(monster1, monster2):
    print("-----------------")
    print("SPEAROW      HP: {}/{}".format(current_health_2, health_2))
    print(" ")
    print(" ")
    print("PIKACHU      HP: {}/{}".format(current_health_1, health_1))
    user_choice = str.lower(input("What attack would you like to choose? (Tackle, Thundershock, or Heal) "))
    print("You used {}".format(user_choice))


class monster1:
# Method for the stats for monster 1
def __init__(self, health_1, current_health_1, attack_1, magic_1, defense_1, magic_defense_1, speed_1):
    self.health_1 = health_1
    self.current_health_1 = current_health_1
    self.attack_1 = attack_1
    self.magic_1 = magic_1
    self.defense_1 = defense_1
    self.magic_defense_1 = magic_defense_1
    self.speed_1 = speed_1

# Method for the attack of monster 1
def attack(self, monster2):
    while True:
        standby_battle_text(monster1, monster2)
        if 'user_choice' == "tackle":
            #Tackle(attack) is True
            damage_calculator(monster1, monster2)
            #Tackle(attack) is False
            #current_health_2 -= DamageDealt
            break
        if 'user_choice' == "thundershock":
            #Thundershock(attack) is True
            damage_calculator(monster1, monster2)
            #Thundershock(attack) is False
            #current_health_2 -= DamageDealt
            break
        if 'user_choice' == "heal":
            current_health_1 += 50
            if current_health_1 >= health_1:
                current_health_1 = health_1
            break
        else:
            print("That is not a valid move! Please choose again.")
            print(" ")


class monster2:
    def __init__(self, health_2, current_health_2, attack_2, magic_2, defense_2, magic_defense_2, speed_2):
    self.health_2 = health_2
    self.current_health_2 = current_health_2
    self.attack_2 = attack_2
    self.magic_2 = magic_2
    self.defense_2 = defense_2
    self.magic_2 = magic_defense_2
    self.speed_2 = speed_2

# Method for the attack of monster 2
def attack(self, monster1):
    if current_health_2 <= 50:
        enemy_heal_chance = randint(0, 3)
        if enemy_heal_chance == 0 or enemy_heal_chance == 1:
            current_health_2 += 50
    else:
        wild_choice = randint(0,1)
        if wild_choice == 0:
            #Tackle(attack) is True
            damage_calculator(monster1, monster2)
            #Tackle(attack) is False
            #current_health_1 -= DamageDealt
        if wild_choice == 1:
            #Peck(attack) is True
            damage_calculator(monster1, monster2)
            #Peck(attack) is False
            #current_health_1 -= DamageDealt

Pikachu.append(monster1(health_1, current_health_1, attack_1, magic_1, defense_1, magic_defense_1, speed_1))
Spearow.append(monster2(health_2, current_health_2, attack_2, magic_2, defense_2, magic_defense_2, speed_2))
  • There are single quotes around user_choice ( 'user_choice' ). Remove them.
  • You are not returning any value from standby_battle_text() function. Hence, there is no way user_choice will be recognised inside attack() method of monster1 . So, you should return user_choice from standby_battle_text()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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