Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement 我的if/elif语句仅在以下情况下返回_If Statement_Python 3.x_While Loop - Fatal编程技术网

If statement 我的if/elif语句仅在以下情况下返回

If statement 我的if/elif语句仅在以下情况下返回,if-statement,python-3.x,while-loop,If Statement,Python 3.x,While Loop,我只想说我刚刚开始编程,只对学习感兴趣 在一个非常简单的回合制格斗游戏中,我给我的玩家选择攻击或防御,但当我输入“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 forr

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

在一个非常简单的回合制格斗游戏中,我给我的玩家选择攻击或防御,但当我输入“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()
随机导入
导入时间
play='y'
名称=“”
playerHp=20
攻击=4
enemyHp=0
enemyAttack=0
def简介():
全局名称
打印(“”)
欢迎来到恐怖的福尔斯特!
危险的怪物生活在这里
区域,您必须销毁或删除该区域
被摧毁!进攻和防守
正确的话,你可能会活下来
时间。睡眠(2)
打印(‘冒险家,你叫什么名字?’)
名称=输入()
def():
x=random.randint(3,5)
全球enemyHp
全方位攻击
enemyHp=x*5
灌肠攻击=x
打印(“一个地精跳出来攻击你,准备战斗!”)
def battle():
全球enemyHp
全方位攻击
全球演奏家
全球攻击
当enemyHp>0和playerHp>0时:
打印('您有'+str(playerHp)+'剩余生命值')
时间。睡眠(2)
打印('妖精有'+str(enemyHp)+'生命值剩余')
时间。睡眠(2)
打印(‘(a)t回退或(d)结束)
选择=输入()
如果选项==“a”或“攻击”:
finalattack=attack+random.randint(-1,2)
打印('你向地精挥杆并做'+str(最后一击)+'伤害')
时间。睡眠(2)
打印('妖精以'+str(敌人攻击)+'伤害'攻击你)
playerp=playerp-敌人攻击
enemyHp=enemyHp-最终目标
选择=0
elif choice==“d”或“defect”:
打印(“妖精向你袭击!”)
打印('但你阻止了他的攻击')
治疗=随机。随机(5,6)-灌肠攻击
playerHp+=治疗
打印('你治疗'+str(治疗)+'Hp')
选择=0

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

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

你的原始声明:

elif choice == 'a' or 'attack':
将始终产生
True
,因为字符串
'attack'
实际上是
True
(更不用说它应该是
if
而不是
elif
;感谢@chepner发现了这一点)


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

是的,您需要使用以下工具测试他们的选择:

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

你的原始声明:

elif choice == 'a' or 'attack':
将始终产生
True
,因为字符串
'attack'
实际上是
True
(更不用说它应该是
if
而不是
elif
;感谢@chepner发现了这一点)


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

是的,您需要使用以下工具测试他们的选择:

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

你的原始声明:

elif choice == 'a' or 'attack':
将始终产生
True
,因为字符串
'attack'
实际上是
True
(更不用说它应该是
if
而不是
elif
;感谢@chepner发现了这一点)


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

是的,您需要使用以下工具测试他们的选择:

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

你的原始声明:

elif choice == 'a' or 'attack':
将始终产生
True
,因为字符串
'attack'
实际上是
True
(更不用说它应该是
if
而不是
elif
;感谢@chepner发现了这一点)

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

问题是

elif choice == 'a' or 'attack':
不是做你想做的事。Python的解析器将其评估为:

elif bool(选项==“a”)或bool(“攻击”):

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

如果选择='a'或选择='attack':

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

问题是

elif choice == 'a' or 'attack':
不是做你想做的事。Python的解析器将其评估为:

elif bool(选项==“a”)或bool(“攻击”):

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

如果选择='a'或选择='attack':

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

问题是

elif choice == 'a' or 'attack':
不是做你想做的事。Python的解析器将其评估为:

elif bool(选项==“a”)或bool(“攻击”):

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

如果选择='a'或选择='attack':

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

问题是

elif choice == 'a' or 'attack':
不是做你想做的事。Python的解析器将其评估为:

elif bool(选项==“a”)或bool(“攻击”):

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

如果选择='a'或选择='attack':


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

看起来您缺少了一个“您发布的代码似乎缺少一些文本;在第一个
elif
子句之前没有
if
语句,这似乎与前面的
print
函数调用至少缺少一个结束引号有关;在第一个
elif
子句之前没有
if
语句,这似乎与前面的
print
函数调用至少缺少一个结束引号有关;在第一个
elif
子句之前没有
if
语句,这似乎与前面的
print
函数调用至少缺少一个结束报价有关。看起来您缺少一个“您的pos”