저는 파이썬 프로그래밍에 대해 매우 새로운 것입니다. 나는 그 순간에 기초를 배우는 중이라는 매우 간단한 간단한 싸움 게임을 텍스트 (텍스트 기반)로 작성하고 있습니다. 내 게임 코드를 아래에 두었습니다 (아직 끝나지 않았습니다), 문제는 프로그램을 실행할 때마다이 오류가 발생했을 때 재생할 문자를 입력 할 때입니다.Python 프로그래밍에 익숙하지 않은 사람이이 프로그램의 오류를 설명해 주시겠습니까?
Traceback (most recent call last):
File "C:\Python26\combat.py", line 60, in <module>
first_player.attack(second_player)
TypeError: 'int' object is not callable
내 게임의 코드는 다음과 같습니다 (별로 크지는 않으니 걱정하지 마십시오!). 나는 선수가 이름을 선택합니다 후에 내가 클래스의 추가 이해하고 문제가 해결되지 않는 이유를 알고 싶어하고 싶은 있지만, 목록에 문자를 추가하는 등의 해결 방법이 있습니다 알고
import time
import random
class player(object):
def __init__(self, name):
self.account = name
self.health = random.randint(50,100)
self.attack = random.randint(30,40)
self.alive = True
def __str__(self):
if self.alive:
return "%s (%i health, %i attack)" % (self.account, self.health, self.attack)
else:
return self.account, "is dead!"
def attack(self, enemy):
print self.account, "attacks", enemy.account, "with %s attack!" % self.attack
enemy.health -= self.attack
if enemy.health <= 0:
enemy.die()
def die(self):
print self.account, "dies!"
alive_players = 2
name1 = raw_input("Enter a name: ")
name2 = raw_input("Enter another name: ")
player_list = {"a":player(name1), "b":player(name2)}
while alive_players == 2:
print
for player_name in sorted(player_list.keys()):
print player_name, player_list[player_name]
print
player1 = raw_input("Who would you like to play as? (a/b): ").lower()
try:
first_player=player_list[player1]
except KeyError, wrong_name:
print wrong_name, "does not exist!"
continue
if first_player==player(name1):
second_player=player(name2)
else:
second_player=player(name1)
time.sleep(1)
print
print "*" * 30
first_player.attack(second_player)
second_player.attack(first_player)
! 가능한 경우 누군가가 잘못을 설명하고 어떻게 해결할 수 있습니까? 나는 이것을 3 일 동안 보았고, 다르게 할 수 있고 효과를 낼 수 있었지만 이것이 왜 처음에는 효과가 없는지 이해하고 싶습니다!
미리 감사드립니다. -Charlie
codereview.stackexchange.com에 대한 질문이 아닙니까? – tzot