python字典添加函数_Python字典函数添加和删除的问题

更新以下内容

我正在Zybooks上的编程课程介绍。我在本章的最后一个项目中,由于间距或返回问题,输出一直失败。我一直在调整它,试图改变事情,但它总是以失败告终。有人愿意看看这个给我一个提示,告诉我我在哪里失败?我真的很感激,谢谢!在

以下是解决问题的步骤:Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey

numbers and the ratings in a dictionary. Output the dictionary's

elements with the jersey numbers in ascending order (i.e., output the

roster from smallest to largest jersey number). Hint: Dictionary keys

can be stored in a sorted list. (3 pts)

Implement a menu of options for a user to modify the roster. Each option is represented by a single character. The program initially

outputs the menu, and outputs the menu after a user chooses an option.

The program ends when the user chooses the option to Quit. For this

step, the other options do nothing. (2 pts)

Implement the "Output roster" menu option. (1 pt)

Implement the "Add player" menu option. Prompt the user for a new player's jersey number and rating. append the values to the two

vectors. (1 pt)

Implement the "Delete player" menu option. Prompt the user for a player's jersey number. Remove the player from the roster (delete the

jersey number and rating). (1 pt)

Implement the "Update player rating" menu option. Prompt the user for a player's jersey number. Prompt again for a new rating for the

player, and then change that player's rating. (1 pt)

Implement the "Output players above a rating" menu option. Prompt the user for a rating. Print the jersey number and rating for all

players with ratings above the entered value. (2 pts)

我的代码:d={}

for i in range(5):

jersey=int(input("Enter player %s's jersey number:\n" % str(i+1)))

rating=int(input("Enter player %s's rating:\n" % str(i+1)))

if jersey not in d:

d[jersey]=rating

print()

print("ROSTER")

for k,v in sorted(d.items()):

print("Jersey number: %d, Rating: %d" % (k,v))

while True:

print('\nMENU')

print('a - Add player')

print('d - Remove player')

print('u - Update player rating')

print('r - Output players above a rating')

print('o - Output roster')

print('q - Quit\n')

choice=input('Choose an option:\n')

choice=choice.lower()

if choice=='o':

print("ROSTER")

for k,v in sorted(d.items()):

print("Jersey number:%d,Rating:%d" % (k,v))

elif choice=='a':

jersey=int(input("Enter a new player jersey number:\n"))

rating=int(input("Enter player's rating:\n"))

if jersey not in d:

d[jersey]=rating

else:

print("\nThe Player already in the list")

elif choice== 'd':

jersey=int(input("\nEnter a jersey number:\n"))

if jersey in d:

del d[jersey]

else:

print("\nThe Player is not in the list")

elif choice== 'u':

jersey=int(input("\nEnter a jersey number:\n"))

if jersey in d:

rating=int(input("\nEnter a new rating for the player:\n"))

d[jersey]=rating

else:

print("\nThe Player is not in the list")

elif choice== 'r':

rating=int(input("\nEnter a rating:\n"))

for k,v in sorted(d.items()):

if v > rating:

print("Jersey number:%d,Rating:%d" % (k,v))

elif choice=='q':

break

这就是我得到的结果。在

这是他们想要的结果:

更新:

最后我改变了整个代码的下部。这解决了我的退货问题,但现在我遇到了其他问题。菜单的两个功能是在字典中添加和删除条目。我又复习了一遍课文,我认为它们的编码是正确的,但它们不起作用。谁能告诉我我哪里出错了吗?谢谢!在

代码如下(仅下半部分,上半部分相同):

^{pr2}$

这是我当前的输出:

这是他们想要的输出。你可以看到应该有一个额外的条目。

抱歉,不知道为什么粘贴格式变得奇怪。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值