2016-07-11 2 views
0

위의 오류가 발생합니다. 단순한 텍스트 기반 게임 일뿐입니다. 다른 사람에,Python 오류 : ValueError : randrange()에 대한 정수가 아닌 arg 1

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

내가 데프 런 (의 오류) :

여기에 코드의 진술. 같은 라인 : EnemyAttack = random.randint (enemy.attack/2, enemy.attack)

아이디어가 있습니까? 내 문제에 대해 더 자세히 설명해야합니까? 고마워요 :)

+1

0.5*randrange(enemy.attack, 2*enemy.attack)로 단순화 할 수있는 완전한 역 추적 –

답변

0

EnemyAttack = random.randint(math.floor(enemy.attack/2), enemy.attack)을 사용하거나 을 사용하여 enemy.attack/2의 결과를 반올림합니다.

randrange()은 전체 정수를 필요로합니다. enemy.attack/2의 결과가 정수가 아니기 때문에 오류가 발생합니다. enemy.attack이 홀수 일 때마다 소수 자릿수가 있습니다. 하나의 옵션은 소수점 결과가 필요한 경우 계산됩니다 후에 규모를 조정하는 것입니다. 예를 들어

: 0.5*randrange(2*(enemy.attack/2), 2*enemy.attack),

+2

왜 그냥 enemy.attack //이 보여? enemy.attack이 정수이면 항상 정수입니다. –

+0

이 경우 파이썬은 다소 간단한 해결책을 가지고 있으며, 꼭 그 사용을 권장합니다. 다른 많은 언어들은 그렇지 않습니다. 그래서 나는 언어에 구속받지 않는 접근법을 지적하는 것이 장점이라고 생각합니다. – Aaron3468

+0

@ Aaron3468, 당신의 대답은 저에게 적의 공격력에 2로 균등하게 짝수 인 숫자를 부여하는 아이디어를주었습니다. 감사합니다 :) – gurgy11

관련 문제