Narks
Narks

Reputation: 71

Python text fight: printing on one line

I am having a problem on line 37 where I try to type a bunch of print statements on one line. One to tell you something, one with a choice statement and another with the variable enemy11. How could I print all that on one line?

Also, with the random choice, say it chooses to punch, how can I detect that so I can take it away from your health? So it chooses punch. It recognizes it punched and takes away punch from your 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!")

Upvotes: 3

Views: 185

Answers (8)

Pradeep Kanegave
Pradeep Kanegave

Reputation: 11

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")

Upvotes: 0

Lake
Lake

Reputation: 1155

Change the list off attacks to a dictionary associating them with their damage:

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

Then use the result of choice() to lookup the amount of damage to do.

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

You can use the user's input in fight1 to lookup their damage as well, but will need to handle when they don't choose a valid option:

try:
    thisAttack = attacks[fight1]
except KeyError as e:
    print "You're not that flexible."
    continue #restart the While loop

Upvotes: 0

Thomas Fenzl
Thomas Fenzl

Reputation: 4392

There are several options, I usually go for string formatting, as you can specify meaningful names for your parameters:

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

You should check out the tutorial on python2.7 or python3 for advanced formatting options.

Upvotes: 1

Turb0247
Turb0247

Reputation: 31

I am new to Python too. Try this:

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

Upvotes: 2

ShitalSavekar
ShitalSavekar

Reputation: 379

So this is your line -

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

You can get the "choice(attack)" variable in some temp variable and then print..

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

Upvotes: 1

shyam
shyam

Reputation: 9368

you could use formatted strings like so

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

Upvotes: 0

ashokadhikari
ashokadhikari

Reputation: 1200

This will work:

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

Upvotes: -1

Mark Tolonen
Mark Tolonen

Reputation: 177610

Separate items with a comma to combine them on one line.

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

Also see the documentation for print and format.

Upvotes: 0

Related Questions