所以我試圖做一個代碼來計算測驗中兩個玩家之間的分數差異。有第一輪比分和第二輪比分。如何將輸出中的列表中的兩個元素配對?
這是我到目前爲止有:
import math
option = input ('How many players?: ')
playerlist = []
firstroundlist = []
secondroundlist = []
j = 0
while j < option:
name = raw_input('Enter player\'s name: ')
playerlist.append(name,)
firstround = input ('Enter first round score: ')
firstroundlist.append(firstround,)
secondround = input('Enter second round score: ')
secondroundlist.append(secondround,)
j += 1
y=0
z=1
dis=[]
while z<len(playerlist):
comparison = (firstroundlist[y]+secondroundlist[y])-(firstroundlist[z]+secondroundlist[z])
print playerlist[y], '-', playerlist [z], 'is', comparison
z+=1
z==0
y+=1
但是,讓我們說,我輸入了四種不同的球員得分,輸出此代碼會是這樣的:
Player 1 - Player 2 is #scoredifference
Player 1 - Player 3 is #scoredifference
Player 1 - Player 4 is #scoredifference
的問題是,我該如何修改代碼,以便它會是這樣的:
Player 1 - Player 2 is #scoredifference
Player 1 - Player 3 is #scoredifference
Player 1 - Player 4 is #scoredifference
Player 2 - Player 3 is #scoredifference
Player 2 - Player 4 is #scoredifference
Player 3 - Player 4 is #scoredifference
備註:請勿使用while循環。在範圍(選項)中使用':j替代('range(1,len(playerlist))'爲第二個循環)。 – 2012-03-25 02:40:52