为什么不定义我的变量?

时间:2015-07-19 19:33:12

标签: python

# players stats and elements
player_health = 100
player_attack.earth = 20
player_attack.air =10
player_attack.fire = 15
player_attack.water = 12
ogre_health = 75
ogre_attack = 15

# main code
player_name = input('What is your name, Guardian? ')

print('Hello ' + player_name + ' the Guardian')

player_element = input('Would you like to be the Guardian of Air, Earth, Fire or Water? ')

if player_element == 'Air':
    print('You have been granted the powers of the Air, Guardian.\
You now have the powers of the Wind and Sky.')

if player_element == 'Earth':
    print('You have been granted the powers of the Earth, Guardian.\
You now have the powers of the Earth.')

if player_element == 'Fire':
    print('You have been granted the powers of Fire, Guardian.\
You now have the powers of Fire. Do not destroy as you wish.')

if player_element == 'Water':
    print('You have been granted the powers of Water, Guardian.\
You now have the powers to control the oceans and water.')

print('There is an enemy in the distance! What do you do?')

player_action = input('What do you do ' + player_name + '? ' + 'Type A to attack ')
if player_action == 'A':
   enemy_health = enemy_health - power

print('The enemy\'s health is at ' + str(enemy_health) + ' Keep attacking!')

player_action = input('What do you do ' + player_name + '? ' + 'Type A to attack ')
if player_action == 'A':

    if player_element == 'Water':
        ogre_health = ogre_health - player_attack.water

print('The enemy\'s health is at ' + str(ogre_health) + ' Keep attacking!')

这是我的代码。 它给了我错误cannot define player_attack.earth 为什么?我定义了它。这里有什么问题? 我正在使用python 3.4.3

1 个答案:

答案 0 :(得分:1)

答案很简单,因为变量名不能有'。'在里面。这将指向一个类中的属性。

您可以在此处找到有关允许的字符和命名规则的更多详细信息:https://docs.python.org/2/reference/lexical_analysis.html#identifiers