[英]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.