2016-07-11 6 views
0

Ich bekomme den obigen Fehler in meinem Code. Es ist nur ein einfaches textbasiertes Spiel.Python Fehler: ValueError: Nicht-integer arg 1 für randrange()

Hier ist der 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() 

ich den Fehler in def run get(), in der sonst: Aussage. Die Zeile, die wie folgt lautet: EnemyAttack = random.randint (feind.angriff/2, feind.angriff)

Irgendwelche Ideen? Sollte ich mehr auf mein Problem eingehen? Danke :)

+1

zeigen die kompletten Zurückverfolgungs –

Antwort

0

Verwenden Sie EnemyAttack = random.randint(math.floor(enemy.attack/2), enemy.attack), oder verwenden Sie math.ceil(), um das Ergebnis von enemy.attack/2 zu runden.

erwartet Ganzzahlen. Der Fehler tritt auf, weil das Ergebnis enemy.attack/2 keine ganze Zahl ist; Es hat Dezimalstellen, wenn enemy.attack ungerade ist. Eine Option besteht darin, nach zu skalieren, es wird berechnet, wenn Sie dezimale Ergebnisse benötigen.

Zum Beispiel: 0.5*randrange(2*(enemy.attack/2), 2*enemy.attack), die in 0.5*randrange(enemy.attack, 2*enemy.attack) vereinfacht werden können

+2

warum nicht nur enemy.attack // 2? Das ist immer eine Ganzzahl, wenn enemy.attack eine Ganzzahl ist. –

+0

In diesem Fall scheint Python eine ziemlich einfache Lösung zu haben und ich empfehle es auf jeden Fall. Viele andere Sprachen nicht, also denke ich, dass das Hervorheben sprachunabhängiger Ansätze auch Vorteile hat. – Aaron3468

+0

@ Aaron3468, Ihre Antwort gab mir die Idee, einfach den Feind Angriffsstärken gerade Zahlen geben, die gleichmäßig durch zwei teilbar sind. Danke :) – gurgy11