简体   繁体   English

如果满足特定条件,是否可以将值添加到列表中?

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

Is it possible to make a code that shows you the position you are in after entering your score and name?是否可以制作一个代码,在输入您的分数和姓名后显示您所在的位置?

#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)

The problem with my current code is I am unable to find the exact position the number can go into, thus when doing .append() I am only able to bring it to the back of the code.我当前代码的问题是我无法找到数字可以进入的确切位置,因此在执行.append()我只能将它带到代码的后面。 If I were to use index, they will give me a value error if my score is not in the list eg if my score is 67, they cant return me a position .如果我使用索引,如果我的分数不在列表中,他们会给我一个值错误, eg if my score is 67, they cant return me a position

Expected Result:预期结果:

#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']]


Essentially the program will check your score with the scores in name_score , and if your score is 65 , you will end up in position 4, and the scores after that will have to shift down by 1, with ['Down', '53'] out of the score board.本质上,程序会用name_score的分数检查你的分数,如果你的分数是65 ,你将在第 4 位结束,之后的分数必须向下移动 1,用['Down', '53']出计分板。

Firstly, there are some tools in the standard library that are useful for similar problems, but I couldn't figure out how to make them work here:首先,标准库中有一些工具对类似问题很有用,但我不知道如何让它们在这里工作:

  • deque is basically a list with a limited length, but it won't let you insert if it's already full deque基本上是一个长度有限的列表,但如果它已经满了,它不会让你插入
  • bisect_right lets you find the point in a sorted list where a new element should be inserted, but it doesn't seem to support a reverse-sorted list bisect_right可让您在排序列表中找到应插入新元素的点,但它似乎不支持反向排序列表

So I wrote a function place() that finds where a score would go, then it's just a matter of list.insert() then list.pop() to remove the last item.所以我写了一个函数place()来查找分数的位置,然后只需要list.insert()然后list.pop()来删除最后一个项目。 If place() hits the end of the list without a match, it errors.如果place()在没有匹配的情况下到达列表的末尾,则会出错。

Sidenote: I don't know why you're storing the numbers as strings, so I made them ints for this example.旁注:我不知道你为什么将数字存储为字符串,所以我在这个例子中将它们设为整数。 I also shortened the example scoreboard for readability and made the items tuples since this is exactly what tuples are for.我还缩短了示例记分板以提高可读性,并将项目元组制作为元组,因为这正是元组的用途。

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()

Output:输出:

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.

相关问题 (Python,Selenium)是否只有在属性满足条件时才能获取文本列表? - (Python, Selenium) Is it possible to get text list only if the attribute meets criteria? 获取符合条件的列表的百分比 - Get percentage of a list that meets criteria 编写代码以检查密码是否符合特定条件 - Writing code to check if a password meets certain criteria 在 Python 中验证数字序列是否满足某些条件 - Validate That A Sequence of Numbers Meets Certain Criteria in Python 检查 pandas 列中的语法是否符合某些条件 - check if syntax in pandas column meets certain criteria 在对象满足特定条件后发送电子邮件 - Send an email once object meets certain criteria Python Pandas:如果groupby中任何前面的行中的值满足特定条件,则从数据框中删除一行 - Python Pandas: Eliminate a row from a dataframe if a value in a any preceding row in a groupby meets a certain criteria Python Pandas-获得符合标准的第一个价值 - Python Pandas - Get the First Value that Meets Criteria 如何在满足某些条件的列表中找到第 n 个连续值的索引? - How can I find the index of the n-th sequential value in a list that meets some criteria? 查找符合条件的第一个值,而不是其索引 - Find the first value that meets criteria, not its index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM