在字典中找到与变量匹配的值

时间:2019-03-13 11:13:47

标签: python

所以我是初学者的极端情况。我的情况是,我必须编写一个程序,以学生的名字和猜测为准,然后在添加所有学生及其各自的猜测后显示赢家。我的添加过程正常运行。我唯一的问题是试图找到比赛的获胜者,那就是猜测罐中的棒棒糖数量。我只是找不到找到最接近的猜测的方法。数小时的搜索使我无济于事。有更好的方法可以做到这一点吗?请帮忙,我将在下面附上我的代码(对不起,如果它不符合标准)

#This program is for the guessing competition. Which is designed to electronically add, display and sort guesses
#
#By D.East 06/03/2019

import sys, os #Giving the program the capabilities to interact with the hardware and operating system

choice = {} #Declaring a dictionary called choice. This will be used for the selection process in the main menu
info = {}
actual = 0
limit = 0
#****************************************************************************
def ClrScr(): #This function uses the OS command line to clear the screen by using the respective command based on the OS
    os.system("clear") # Linux - OSX
    os.system("cls") # Windows

def Quit(): #This function terminates the python interpreter causing the program to exit as such
    ClrScr()
    SystemExit

def SplashScreen(): #Splash screen to introduce the user to the program
    print('********GUESSING GAME********')
    print('-----------------------------')
    print()
    print()
    print('Guess the closest and you could WIN!!!!')
    print()
    print()
    print()
    print()
    print()
    print()
    print()
    print('Press any key to continue >>>>')
    input()
    ClrScr()

def MainMenu(): #Main menu where users make their selection for what they wish to do
    choice = { #The "choice" dictionary is given values before the showing of any text
        1 : AddGuesses,
        2 : DisplayGuesses, #Each of these functions take the user to their respective module
        3 : DisplayWinners,
        4 : ResetData,
        5 : Quit,
    }
    print('********GUESSING GAME*********')
    print('------------------------------')
    print()
    print()
    print('What would you like to do?')
    print()
    print('1) : Add Guesses')
    print('2) : Display Guesses')
    print('3) : Display Winners') #Users are given a shown a selection of options on what the program can do
    print('4) : Reset Data')
    print('5) : Exit')
    sel = int(input('Selection: ')) #This takes the users selection, changes it to an integer, then stores it in its designated variable
    while sel > 5:
        sel = int(input('Selection: '))    
    choice[sel]() #The previous variable is then used as the index number to call on the respectve function based on the varibles value
    ClrScr()

def AddGuesses(): #This module adds guesses to a dictionary that is made when the program starts
    limit = 0
    guess = 0
    name = '' #Declaring variables necessary
    ClrScr()
    print('********GUESS ADDING MENU********')
    print('-----------------------------')
    print()
    print()
    print('This is the where guesses are added')
    print()
    print('To stop the adding process, enter 999 into the prompt')
    input('Press any key to continue >>>>')
    while name != '999': #Just a while statement to make things work
        ClrScr()
        name = str(input('Student Name: ')) #Stores the students name in a variable for later use
        if name != '999': #This stops the student add process by checking if the value the user put in was 999, which halts this while loop
            guess = int(input('Student Guess: '))
            info[name] = guess #Using the name variable as the key name and we can store the guess as the index whilst using the students name as the unique identifier
            limit = limit + 1 #This variable is used for a later part of the program for when a certain function will be sanning the dictionary. This is to stop errors later in the program
            print()
            print(name, ' - ', info[name]) #This is to show the user what they've entered into the terminal
            print('Student added')
            input('Press any key to continue >>>>')
    ClrScr()
    actual = int(input('What is the correct number: '))
    ClrScr()
    MainMenu()

def DisplayGuesses():
    ClrScr()
    print('****DISPLAY GUESSES********')
    print('---------------------------')
    print()
    for a in info:
        print(a)
        a = info[a]
        print(a)
        #for b in info:
        #    b = info[b]
         #   print(b)
    print('---------------')
    input()
    ClrScr()
    MainMenu()
#*************************************************************************    
def DisplayWinners(): 
    ClrScr()
    for value in info.values():
        if value != actual:
    #This is the part i cannot figure out, this is a partial attempt that didn't get me anywhere     


    input('test')
    ClrScr()
    MainMenu()
#********************************************************************  
def ResetData():
    ClrScr()
    print('This is the ResetData module')
    input()
    ClrScr()
    MainMenu()

if __name__ == '__main__':
    ClrScr()
    SplashScreen()
    MainMenu()

0 个答案:

没有答案