【问题标题】:My if/elif statement only returns if [duplicate]我的 if/elif 语句仅返回 if [重复]
【发布时间】:2014-05-13 23:57:27
【问题描述】:

我只想说我才刚刚开始编程,只对学习感兴趣。

在一个非常简单的回合制格斗游戏中,我让玩家选择攻击或防御,但是当我输入“d”表示防御时,它会返回攻击代码。

import random
import time

play = 'y'

name = ' '

playerHp = 20
attack =  4
enemyHp = 0
enemyAttack = 0


def intro ():
    global name
    print('''
welcome to the scary forrest!
dangerous monsters live in this
area and you must destroy or
be destroyed! attack and defend
correctly and you may survive.''')
    time.sleep (2)

    print ('what is your name, adventurer?')

    name = input()

def randomEnemy ():
    x = random.randint (3,5)
    global enemyHp
    global enemyAttack
    enemyHp = x * 5
    enemyAttack = x
    print('''A goblin has jumped out to attack you, prepare to fight!''')


def battle ():
    global enemyHp
    global enemyAttack
    global playerHp
    global attack

    while enemyHp > 0 and playerHp > 0:
        print ('you have ' + str(playerHp) + ' health left')
        time.sleep (2)
        print ('the goblin has ' + str(enemyHp) + ' health left')
        time.sleep (2)
        print ('(a)ttack or (d)efend)

        choice = input()

        if choice == 'a' or 'attack':
            finalattack = attack + random.randint (-1,2)
            print ('you swing at the goblin and do ' + str(finalattack) + ' damage')
            time.sleep(2)
            print ('the goblin strikes you for ' + str(enemyAttack) + ' damage')
            playerHp = playerHp - enemyAttack
            enemyHp = enemyHp - finalattack
            choice = 0

        elif choice == 'd' or 'defend':
            print ('the goblin strikes at you!')
            print ('but you block his attack')
            heal = random.randint (5,6) - enemyAttack
            playerHp += heal
            print ('you heal for ' + str(heal) + ' Hp')
            choice =0

     if playerHp <= 0:
        print('you lose... noone finds your body because the \ngoblin drags you into  his cave')

    if enemyHp <= 0:
    print ('you stand victorious over your foe, but noone is \nreally impressed except yo\' momma')

intro()

randomEnemy()

battle()

我的 if 语句有问题吗?

如果有人可以帮助我并使用小词,将不胜感激。

【问题讨论】:

  • 你好像少了一个'
  • 您发布的代码似乎缺少一些文字;第一个elif 子句之前没有if 语句,这似乎与前面的print 函数调用至少缺少结束引号有关。

标签: if-statement python-3.x while-loop


【解决方案1】:

是的,您需要测试他们的选择:

if choice == 'a' or choice == 'attack':

和:

elif choice == 'd' or choice == 'defend':

您的原始陈述:

elif choice == 'a' or 'attack':

将始终产生True,因为字符串'attack' 实际上是True(更不用说它应该是if 而不是elif;感谢@chepner 发现)。

(我没有检查你所有的代码)。

【讨论】:

  • +1 他的代码中缺少if 本身,但它很可能表现出与elifs 中所示类似的问题。
  • @chepner 不错不错。
【解决方案2】:

问题是

elif choice == 'a' or 'attack':

没有按照你的想法做。 Python 的解析器将其评估为:

elif bool(choice == 'a') 或 bool('attack'):

且非空字符串的布尔值为 True。你想要的是:

如果选择 == 'a' 或选择 == '攻击':

请注意,我已将 elif 替换为一个简单的 if,因为它是第一个。 (elif 是 else if 的缩写)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多