根据下一个输入中指示的数字输入和返回列表

时间:2014-05-30 04:45:45

标签: python

再试一次..

我试图让它成为用户提供8个职业篮球队的名字,当被问及他们在nba选择中的排名时,它会显示他们获得多少轮次的位置。同时每8支球队扭转位置。 I.E如果一支球队是第一轮选秀,那么第8支球队将在第9轮首选。

nba = ""
count = 1
teams = []
while count < 9:
    nba = input("enter nba team: ")
    count = count + 1
    teams.append(nba)
selection = input("how many rounds will this go to? ")
print("The team order is: ")

样本 -

输入1:开拓者

输入2:湖人队

输入3:凯尔特人队

输入4:加热

输入5:网

输入6:战士

输入7:cavs

输入8:mavs

你要去几轮? 11

第1轮:开拓者

第二轮:湖人队

第3轮:凯尔特人队

第四轮:加热

第5轮:网

第6轮:勇士

第7轮:骑士

第8轮:mavs

第9轮:mavs

第10轮:骑士

第11轮:勇士

抱歉,如果这有点令人困惑。

2 个答案:

答案 0 :(得分:1)

def print_rounds(rounds, team, cur_round=1):
    if rounds < len(team): #Handle the case when rounds is less than what is left.
        for i in team[:rounds]:
            print "Round: ", cur_round,
            print i
            cur_round += 1
        return
    for i in team:
        print "Round: ", cur_round,
        print i
        cur_round += 1
    rounds -= len(team)
    print_rounds(rounds, team[::-1], cur_round=cur_round) #Recursive call with the team list reversed.

teams = ["Blazers", "Lakers", "Celtics", "Heat", "Nets", "Warriors", "Cavaliers", "Mavericks"]

print_rounds(20, teams)

产地:

Round:  1 Blazers
Round:  2 Lakers
Round:  3 Celtics
Round:  4 Heat
Round:  5 Nets
Round:  6 Warriors
Round:  7 Cavaliers
Round:  8 Mavericks
Round:  9 Mavericks
Round:  10 Cavaliers
Round:  11 Warriors
Round:  12 Nets
Round:  13 Heat
Round:  14 Celtics
Round:  15 Lakers
Round:  16 Blazers
Round:  17 Blazers
Round:  18 Lakers
Round:  19 Celtics
Round:  20 Heat

答案 1 :(得分:0)

你可以做这样的事情:

teams = ["Blazers", "Lakers", "Celtics", "Heat", "Nets", "Warriors", "Cavaliers", "Mavericks"]

def print_next(your_team_list):
    counter = 1
    current_index = 0
    for i in range(len(teams)):
        print "Round " + str(counter) + ": " + your_team_list[i]
        counter +=1

所以你的样本输出是:

Round 1: Blazers
Round 2: Lakers
Round 3: Celtics
Round 4: Heat
Round 5: Nets
Round 6: Warriors
Round 7: Cavaliers
Round 8: Mavericks

显然,这个功能只是一个简单的说明性例子。您可以更改它,以便输入问题设置counter