什么地方给出

时间:2015-09-01 12:39:36

标签: python

我刚开始学习python,我正在尝试编写代码,打印出参赛者将参加比赛的地方。只有3个值,您可以看到我在下面所做的事情;

s1 = input('Score 1: ')
s2 = input('Score 2: ')
s3 = input('Score 3: ')

if s1 != s2 and s2 != s3 and s3 != s1:
  print('1st 2nd 3rd')
elif s1 == s2 and s2 == s3 and s3 == s1:
  print('1st 1st 1st')

正如您所看到的,我只需要根据用户的输入找到给出的位置(即第1或第2)。我怎样才能找到所给出的地方是否是第1个第3个'或者' 1st,2nd,2nd&#39 ;?分数越高,您获得的位置越高。

编辑:对不起,以前不清楚,但只是想知道,例如,如果我有10,10和5的值,它应该打印' 1st 1st 3rd'如果值为10,5和5,则应打印第1个第2个第2个'。 我应该怎么做呢?

1 个答案:

答案 0 :(得分:0)

def ranking(num=3):
    scores = []
    for n in range(num):
        msg = 'Score %d' % (n+1)
        scores.append((msg, input('%s: ' % msg)))
    scores.sort(key=lambda rank: rank[1], reverse=True)
    return scores

>>> X = ranking(3)
Score 1: 4
Score 2: 5
Score 3: 8
>>> print X
[('Score 3', 8), ('Score 2', 5), ('Score 1', 4)]

>>> def num2ord(n):         
    return '%dxx' % n #TODO

>>> ppos, pscore = None, None
>>> for pos, (msg, score) in enumerate(X):
    if score != pscore:
        ppos = pos
    pscore = score
    print '%s is %d' % (num2ord(ppos+1), msg)

1xx is Score 2
1xx is Score 3
3xx is SCore 1

函数num2ord可以从this recipe

获取