2013-04-17 73 views
3

我在第37行有一個問題,我嘗試在一行上輸入一串打印語句。一個告訴你一件事,一個帶有選擇聲明,另一個帶變量enemy11。我怎樣才能在一行上打印所有內容?Python文本打鬥:在一行上打印

此外,隨機選擇,說它選擇拳打,我怎麼能檢測到,所以我可以把它從你的健康?所以它選擇了一拳。它承認它被打孔並從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!") 
+0

這是一個非常酷計劃! –

+0

我有很多有趣的編程! – Narks

回答

0

用逗號分隔的項目將它們合併成一行。

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

另請參閱printformat的文檔。

2

我也是Python新手。試試這個:

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

那我一定是supernoob,謝謝! – Narks

+0

歡迎您:) – Turb0247

-1

這將工作:

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

print(「%s%s You」%(enemy11,choice(attacks)))這是正確的方式 – mou

0

你可以使用格式的字符串,像這樣

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

這是你行 -

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

你可以在某些臨時變量中獲得「選擇(攻擊)」變量能夠然後打印..

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

有幾個選項,我通常去字符串格式化,你可以爲你的參數指定有意義的名稱:

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

你應該看看tutorial on python2.7python3用於高級格式化選項。

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 
+0

鑰匙和鑰匙在哪裏進來? – Narks

+0

我只在java – Narks

+0

'choice()'中看到了關鍵的東西,期望一個簡單的列表。 'dict.keys()'返回所有鍵的列表。我們使用'choice()'選項來查找相應的值:'value = dict [key]' http://docs.python.org/2/tutorial/datastructures.html#dictionaries http:///docs.python.org/2/library/stdtypes.html#typesmapping – Lake

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