Python文本斗争:在一行打印

时间:2013-04-17 06:04:19

标签: python

我在第37行遇到问题,我尝试在一行上输入一堆打印语句。一个告诉你的东西,一个带有选择语句,另一个带有变量enemy11。我怎么能在一行上打印所有内容?

此外,随机选择,说它选择打卡,我怎么能检测到这一点,所以我可以把它从健康中拿走?所以它选择了打卡。它识别出它被打了一拳并从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!")

8 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

所以这是你的行 -

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

您可以在某个临时变量中获取“choice(attack)”变量,然后打印..

temp = choice(attack)
print ("%s %s You" % (enemy11, temp))

答案 2 :(得分:1)

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

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

你应该查看tutorial on python2.7python3用于高级格式化选项。

答案 3 :(得分:0)

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

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

另请参阅printformat的文档。

答案 4 :(得分:0)

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

 print('%s %s You' % (enemy11, choice(attacks)))

答案 5 :(得分: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

答案 6 :(得分: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")

答案 7 :(得分:-1)

这将有效:

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