简体   繁体   English

通过值python打印字典键和值

[英]Printing dictionary keys and values by value python

For a python class I am taking I have to create a simple menu that does a few things, one of them being print out all player's jersey numbers and ratings based on rating. 对于python类,我必须创建一个简单的菜单来执行一些操作,其中之一是根据等级打印出所有球员的球衣号码和等级。 When the user selects 'r' from the menu the user is supposed to be able to enter a rating and then the program will print out all jersey numbers and ratings that are equal to or greater than the entered rating from the user. 当用户从菜单中选择“ r”时,应该能够输入一个等级,然后程序将打印出所有等于或大于用户输入的等级的球衣号码和等级。 I thought I had it figured out but I can't seem to get it to work. 我以为我已经弄清楚了,但是我似乎无法使它正常工作。 Any ideas on where I went wrong here? 关于我在这里出错的任何想法?

roster = {}

for i in range(5):
    x = int(input("Enter player number (0-99): "))
    y = int(input("Enter player rating: "))
    roster[x] = y

for i in sorted(roster):
    print('Jersey number:', i, 'Player rating:', roster[i])

sel = 'z'

while sel != 'q':

    sel = input("MENU: \na - Add Player\nd - Remove Player\nu - Update 
    Player Rating\n"
            "r - Output Players Above A Rating\no - Output Roster\nq - 
    Quit\n")

    if sel == 'a':
        x = int(input("Enter player number (0-99): "))
        y = int(input("Enter player rating: "))
        roster[x] = y
    elif sel == 'o':
        for i in sorted(roster):
            print('Jersey number:', i, 'Player rating:', roster[i])
    elif sel == 'd':
        delete = int(input("Enter jersey number to be deleted: "))
        del roster[delete]
    elif sel == 'u':
        update = int(input("Enter jersey number to be updated: "))
        change = int(input("Enter new player rating: "))
        roster[update] = change
    elif sel == 'r':
        above = int(input("Enter a rating: "))
        for key, value in roster.items():
            if above >= value:
                print(key)
    elif sel == 'q':
        print("Thanks for using the program")
    else:
        continue

The user input the value of which we print out the ratings that are higher than that. 用户输入的值将打印出高于该值的等级。 In your code, this value is known as above . 在您的代码中,此值above

Hence if your code, it should be 因此,如果您的代码应该是

if value >= above: rather than if above >= value: if value >= above:而不是if value >= above: if above >= value:

Also, take note of indentatin, it seems to me that you want the if blocks to be inside the while loop. 另外,请注意缩进,在我看来,您希望if块位于while循环内。

your question does not really elaborate on what difficulties you are facing or how the program crashes, if it crashes at all. 您的问题并没有真正说明您面临的困难或程序崩溃(如果完全崩溃)的情况。 But one thing i have noticed is that you are printing ratings that are less than or equal to the provided rating... not above or equal to the rating. 但是我注意到的一件事是您打印的额定值小于或等于提供的额定值...不大于或等于额定值。

so this 所以这

if above >= value:

should be replaced with 应该替换为

if above <= value:

If the issue is different from this please comment back with more explanation on what you are facing and i would be glad to help 如果问题与此不同,请回覆并进一步说明您所面临的问题,我很乐意为您提供帮助

elif sel == 'r':
    above = int(input("Enter a rating: "))
    for key, value in roster.items():
        if above <= value:
            print('Jersey number: ', key, 'Player rating: ', value)

This fixed the code. 这修复了代码。 Thanks @Doodle Dee 谢谢@Doodle Dee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM