根据下一个inpu中指示的编号输入并返回列表

2024-09-29 23:17:16 发布

您现在位置:Python中文网/ 问答频道 /正文

再试一次

我正在尝试这样做,用户给出8个职业篮球队的名字,当被问到他们在nba的排名时,它会显示他们的位置,他们得到了多少圈。同时每8队倒换一次位置。也就是说,如果一个队是第一个选择,第八个队将在第九轮中第一个选择

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:微型飞行器

你要打多少回合?十一,

第一轮:开拓者

第二轮:湖人队

第三轮:凯尔特人

第四轮:热身赛

第五轮:蚊帐

第六轮:勇士队

第七轮:骑士队

第8轮:小牛队

第九轮:小牛队

第十轮:骑士队

第11轮:勇士队

抱歉,这有点让人困惑


Tags: 用户inputcount名字teamenternbawhile
2条回答

你可以这样做:

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由您的输入问题设置

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

相关问题 更多 >

    热门问题