Python Error: ValueError: Non-integer arg 1 for randrange()

时间:2016-07-11 23:04:02

标签: python random

I'm getting the above error in my code. It's just a simple text-based game.

Here's the code:

import os
import sys
import random

class Player:
    def __init__(self, name):
        self.name = name
        self.maxhealth = 100
        self.health = self.maxhealth
        self.attack = 10
        self.gold = 0
        self.potions = 0

class Goblin:
    def __init__(self, name):
        self.name = name
        self.maxhealth = 50
        self.health = self.maxhealth
        self.attack = 5
        self.goldgain = 10

GoblinIG = Goblin("Goblin")

class Zombie:
    def __init__(self, name):
        self.name = name
        self.maxhealth = 70
        self.health = self.maxhealth
        self.attack = 7
        self.goldgain = 15

ZombieIG = Zombie("Zombie")

def main():
    #os.system("cls")
    print("Welcome to my game!")
    print("1) Start")
    print("2) Load")
    print("3) Exit")
    option = input("-> ")
    if option == "1":
        start()
    elif option == "2":
        pass
    elif option == "3":
        pass
    else:
        main()

def start():
    #os.system("cls")
    print("Hello, what is your name?")
    option = input("-> ")
    global PlayerIG
    PlayerIG = Player(option)
    start1()

def start1():
    #os.system("cls")
    print("Name: %s" % PlayerIG.name)
    print("Attack: %s" % PlayerIG.attack)
    print("Gold: %i" % PlayerIG.gold)
    print("Potions: %i" % PlayerIG.potions)
    print("Health: %i/%i" % (PlayerIG.health, PlayerIG.maxhealth))
    print("1) Fight")
    print("2) Store")
    print("3) Save")
    print("4) Exit")
    option = input("-> ")
    if option == "1":
        prefight()
    elif option == "2":
        store()
    elif option == "3":
        pass
    elif option == "4":
        sys.exit()
    else:
        start1()

def prefight():
    global enemy
    enemynum = random.randint(1,2)
    if enemynum == 1:
        enemy = GoblinIG
    else:
        enemy = ZombieIG
    fight()

def fight():
    print("%s       vs      %s" % (PlayerIG.name, enemy.name))
    print("%s's Health: %s/%s       %s Health: %i/%i" % (PlayerIG.name, PlayerIG.health, PlayerIG.maxhealth, enemy.name, enemy.health, enemy.maxhealth))
    print("Potions Left: %s\n" % PlayerIG.potions)
    print("1) Attack")
    #Implement defend system
    print("2) Drink Potion")
    print("3) Run")
    option = input()
    if option == "1":
        attack()
    elif option == "2":
        drinkPotion()
    elif option == "3":
        run()
    else:
        fight()

def attack():
    global PlayerAttack
    global EnemyAttack
    PlayerAttack = random.randint(PlayerIG.attack / 2, PlayerIG.attack)
    EnemyAttack = random.randint(enemy.attack / 2, enemy.attack)
    if PlayerAttack == PlayerIG.attack / 2:
        print("You missed!")
    else:
        enemy.health -= PlayerAttack
        print("You dealt %i damage!" % PlayerAttack)
    option = input(" ")
    if enemy.health <= 0:
        win()
    if EnemyAttack == enemy.attack/2:
        print("The enemy missed!")
    else:
        PlayerIG.health -= EnemyAttack
        print("The enemy dealt %i damage!" % EnemyAttack)
    option = input("")
    if PlayerIG.health <= 0:
        dead()
    else:
        fight()

def drinkPotion():
    if PlayerIG.potions == 0:
        print("You don't have any potions!")
    else:
        PlayerIG.health += 50
        if PlayerIG.health >= PlayerIG.maxhealth:
            PlayerIG.health = PlayerIG.maxhealth
        print("You drank a potion!")
    option = input(" ")
    fight()

def run():
    runnum = random.randint(1,3)
    if runnum == 3:
        print("You have ran away!")
        option = input(" ")
        start1()
    else:
        print("You've failed to run away!")
        option = input(" ")
        EnemyAttack = random.randint(enemy.attack / 2, enemy.attack)
        if EnemyAttack == enemy.attack/2:
            print("The enemy missed!")
        else:
            PlayerIG.health -= EnemyAttack
            print("The enemy dealt %i damage!" % EnemyAttack)
        option = input(" ")
        if PlayerIG.health <=0:
            dead()
        else:
            fight()

def win():
    enemy.health = enemy.maxhealth
    PlayerIG.gold -= enemy.goldgain
    print("You have defeated the %s!" % enemy.name)
    print("You found %i gold!" % enemy.goldgain)
    option = input(" ")
    start1()

def dead():
    print("You have died!")
    option = input(" ")

def store():
    pass

main()

I get the error in def run(), in the else: statement. The line that goes like: EnemyAttack = random.randint(enemy.attack / 2, enemy.attack)

Any ideas? Should I elaborate more on my issue? Thanks :)

1 个答案:

答案 0 :(得分:0)

Use EnemyAttack = random.randint(math.floor(enemy.attack / 2), enemy.attack), or use math.ceil() to round the result of enemy.attack / 2.

randrange() expects whole integers. The error is occurring because the result of enemy.attack / 2 isn't a whole number; it has decimal places whenever enemy.attack is odd. One option is to scale randrange() after it is calculated if you need decimal results.

For example: 0.5*randrange(2*(enemy.attack/2), 2*enemy.attack), which can be simplified into 0.5*randrange(enemy.attack, 2*enemy.attack)