簡體   English   中英

如果滿足特定條件,是否可以將值添加到列表中?

[英]Is it possible to add a value into a list if it meets certain criteria?

是否可以制作一個代碼,在輸入您的分數和姓名后顯示您所在的位置?

#name_score before input and execution 
name_score = [['Never', '69'], ['Gonna', '66'], ['Give', '65'], ['You', '64'], ['Up', '62'], ['Never', '60'], ['Gonna', '59'], ['Let', '55'], ['You', '54'], ['Down', '53']]

name = input('Enter your name: ')
score = int(input('Enter your score: '))

scores = []
for x in name_score:
    scores.append(x[1])

if score in range (int(scores[9]), int(scores[0])):
    name_score.append([name, score])

print(name_score)

我當前代碼的問題是我無法找到數字可以進入的確切位置,因此在執行.append()我只能將它帶到代碼的后面。 如果我使用索引,如果我的分數不在列表中,他們會給我一個值錯誤, eg if my score is 67, they cant return me a position

預期結果:

#program when it is executed *Example 1* 

Enter your name = Mark
Enter your score = 54
Congratulations! You are position 10 on the score board.
 

#name_score after user input
name_score =  [['Never', '69'], ['Gonna', '66'], ['Give', '65'], ['You', '64'], ['Up', '62'], ['Never', '60'], ['Gonna', '59'], ['Let', '55'], ['You', '54'], ['Mark', '54']]


#program when it is executed *Example 2* 

Enter your name = John
Enter your score = 68
Congratulations! You are position 2 on the score board.


#name_score after user input
name_score = [['Never', '69'], ['John', '67'], ['Gonna', '66'], ['Give', '65'], ['You', '64'], ['Up', '62'], ['Never', '60'], ['Gonna', '59'], ['Let', '55'], ['You', '54']]


#program when it is executed *Example 3*

Enter your name = Bob
Enter your score = 40
Sorry, you did not make it into the score board.


#name_score after user input
name_score = [['Never', '69'], ['Gonna', '66'], ['Give', '65'], ['You', '64'], ['Up', '62'], ['Never', '60'], ['Gonna', '59'], ['Let', '55'], ['You', '54'], ['Down', '53']]


本質上,程序會用name_score的分數檢查你的分數,如果你的分數是65 ,你將在第 4 位結束,之后的分數必須向下移動 1,用['Down', '53']出計分板。

首先,標准庫中有一些工具對類似問題很有用,但我不知道如何讓它們在這里工作:

  • deque基本上是一個長度有限的列表,但如果它已經滿了,它不會讓你插入
  • bisect_right可讓您在排序列表中找到應插入新元素的點,但它似乎不支持反向排序列表

所以我寫了一個函數place()來查找分數的位置,然后只需要list.insert()然后list.pop()來刪除最后一個項目。 如果place()在沒有匹配的情況下到達列表的末尾,則會出錯。

旁注:我不知道你為什么將數字存儲為字符串,所以我在這個例子中將它們設為整數。 我還縮短了示例記分板以提高可讀性,並將項目元組制作為元組,因為這正是元組的用途。

scoreboard_example = [
    ('A', 69),
    ('B', 66),
    ('C', 65),
    ('D', 64),
    ('E', 62)]

def place(score, scoreboard):
    for i, (_name, existing) in enumerate(scoreboard):
        if existing < score:
            return i
    else:  # no break
        raise ValueError('did not place')

for test in ('Mark', 64), ('John', 68), ('Bob', 40):
    s = scoreboard_example.copy()
    try:
        position = place(test[1], s)
    except ValueError:
        print('Sorry, you did not make it into the score board.')
    else:
        print(f'Congratulations! You are position {position} on the score board.')
        s.insert(position, test)
        s.pop()
    print(s)
    print()

輸出:

Congratulations! You are position 5 on the score board.
[('A', 69), ('B', 66), ('C', 65), ('D', 64), ('Mark', 64)]

Congratulations! You are position 2 on the score board.
[('A', 69), ('John', 68), ('B', 66), ('C', 65), ('D', 64)]

Sorry, you did not make it into the score board.
[('A', 69), ('B', 66), ('C', 65), ('D', 64), ('E', 62)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM