3

我在第 37 行遇到问题,我尝试在一行上键入一堆打印语句。一个告诉你一些事情,一个带有选择语句,另一个带有变量enemy11。我怎么能在一行上打印所有这些?

另外,对于随机选择,比如说它选择打孔,我怎么能检测到它,这样我就可以把它从你的健康中带走?所以它选择了punch。它识别出它被打了,并从你的 HP 中拿走了拳。

hp=100
enemy1=100
enemy2=200
boss=500
punch=10
kick=20
fatality=99999999

attacks = ['kick', 'punch', 'fatality']
from random import choice


from time import sleep

print("Welcome to Ultimate Fight Club Plus")
sleep(1)
print("What is your name?")
name=raw_input("> ")
print("Good luck"), name
sleep(1)
print("Choose your opponent")
enemy11=raw_input("> ")
print("You chose"), enemy11
sleep(1)
print("his health is"), enemy1
sleep(1)
print("Fight!")
while enemy1>1:
        print("You can kick or punch")
        fight1=raw_input("> ")
        if fight1=="punch":
                enemy1 -= punch
                print("You punch him in the face")
                sleep(1)
                print("His health is now"), enemy1
                sleep(1)
                print(enemy11) print choice(attacks) print("You")
        if fight1=="kick":
                enemy1 -= kick
                print("You kick him.")
                sleep(1)
                print("His health is now"), enemy1
print("You win!")
4

8 回答 8

2

我也是 Python 新手。尝试这个:

print(enemy11, choice(attacks), "You")
于 2013-04-17T06:09:45.150 回答
1

有几个选项,我通常使用字符串格式,因为您可以为参数指定有意义的名称:

print "{who} {action} you".format(who=enemy11, action=choice(attacks))

您应该查看有关 python2.7python3的教程以获取高级格式化选项。

于 2013-04-17T06:24:30.650 回答
1

所以这是你的线 -

print(enemy11) print choice(attacks) print("You")

您可以在某个临时变量中获取“选择(攻击)”变量,然后打印..

temp = choice(attack)
print ("%s %s You" % (enemy11, temp))
于 2013-04-17T06:13:44.443 回答
0
import sys

sys.stdout.write(enemy11)
sys.stdout.write(attacks)
sys.stdout.write("you")

stdout.write prints the matter in same line so for adding spaces in between 
you have to add it seperately...

sys.stdout.write(enemy11)
sys.stdout.write(" ")
sys.stdout.write(choice(attacks))
sys.stdout.write(" ")
sys.stdout.write("you")
于 2013-04-17T06:40:03.827 回答
0

将攻击列表更改为将它们与其损害相关联的字典:

attacks = {'punch':10, 'kick':20, 'fatality':99999999}

然后使用结果choice()来查找要造成的伤害量。

thisAttack = choice(attacks.keys())
hp -= attacks[thisAttack]
print("%s's %s leaves you with %s hp."% (enemy11, thisAttack, hp))

您也可以使用用户的输入fight1来查找他们的损坏,但是当他们没有选择有效选项时需要处理:

try:
    thisAttack = attacks[fight1]
except KeyError as e:
    print "You're not that flexible."
    continue #restart the While loop
于 2013-04-17T06:32:32.563 回答
0

用逗号分隔项目以将它们组合在一行中。

print(enemy11,choice(attacks),"You")

另请参阅 和 的print文档format

于 2013-04-17T06:09:44.567 回答
0

你可以像这样使用格式化的字符串

 print('%s %s You' % (enemy11, choice(attacks)))
于 2013-04-17T06:10:53.820 回答
-1

这将起作用:

print ("%s %s %s" % (enemy11, choice(attacks), "You"))
于 2013-04-17T06:09:56.640 回答