Python, Вложенные элементы словаря, связывающие - PullRequest
0 голосов
/ 13 июня 2018

Я пытаюсь ввести оценки n учеников и найти среднее значение по конкретному ученику.Я использую вложенные элементы словаря и пытаюсь получить ввод в одной строке кода.Оценки варьируются от 0 до 100, и мне нужно сделать это, прежде чем ссылаться на ключи словаря.Ниже мой код, я не выполняю условие диапазона (от 0 до 100).Пожалуйста, помогите.

if __name__ == '__main__':
n = int(input())
if n in range(2,11):
    student_marks = {}
    for _ in range(n):
        line = input().split()
        name = line[0]
        scores =  line[1:]
        scores = list(map(float, scores))

        truth,x,y = 0,0,0
        y = len(scores)
        for x in scores:
            if 0<=x<=100:
                truth = truth+1

        if(truth == y):
            student_marks[name] = scores
        else:
            print("Marks out of range")

    query_name = input()

    add = 0
    m=0
    for s in student_marks[query_name]:
        m = m+1
    if x in student_marks:
        if x == query_name :
            for y in student_marks[query_name]:
                add = add + y
            average = float(add/m)
        else:
            print("Name doesnt exist.Enter correct name and start again")
    else:
         print("The person not ideally linked,since incorrect marks entered,Enter properly and try again")

    print("%.2f" % average)

Ответы [ 3 ]

0 голосов
/ 13 июня 2018

@ Махантеш , как я прочитал и понял из вашего кода, я пытался реализовать вашу проблему по-своему.Я взял разные имена, такие же, как ты.

Я также использовал модуль json для красивой печати словаря.

Пожалуйста, проверьте приведенный ниже код и дайте мне знать, если вам нужны какие-либо изменения.

Не забудьте проверить вывод.

import json 

def get_averages():
    student_marks = {};
    successful_trials = 0; # IT IS NOT REQUIRED, JUST TO MAKE PROGRAM USER FRIENDLY

    while True:
        try:
            # CREATE A LIST 
            # ['Rishikesh', '90', '95', '90', '80', '85', '70']
            line = input('\n('+ str(successful_trials + 1) + ') ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> ').split(); 
            if len(line) > 0: 
                # NAME OF STUDENT  => 'Rishikesh'
                name = line[0]; 

                if name.lower() == 'exit': # IF USER ENTERS ANYTHING STARTING WITH exit (stop iteration)
                    break

                # SCORES OF A STUDENT (INTEGERS) => [90, 95, 90, 80, 85, 70]
                scores = [int(number) if (int(number) >= 0 and int(number) <= 100) else -1 for number in line[1:]] 

                if not scores:
                    print("\nERROR: INPUT SHOULD HAVE  AT LEAST 1 OR MORE SCORES (SPACE SEPARATED)")
                    continue    
            else:
                print("\nERROR: INPUT SHOULD HAVE NAME FOLLOWED BY AT LEAST 1 OR MORE SCORES (SPACE SEPARATED)")
                continue

            if -1 not in scores: # SCORES ARE IN RANGE MEANS IT IS NOT LIKE [90, -1, 90, -1, 85, 70]
                student_marks[name] = {
                    "scores": scores,
                    "average": sum(scores) / len(scores) # AVERAGE
                }
            else:
                print("\nERROR: ENTERED SCORES ARE NOT IN RANGE")
                continue    

            successful_trials += 1; # IF EVERYTHING PASSED
        except Exception as error:
            print('\nERROR OCCURED: ', error)
            print("\nPLEASE ENTER CORRECT VALUE")
            continue

    return student_marks; # DICTIONARY OF DICTIONARIES

if __name__ == "__main__":
    # CALL TO get_averages()
    student_marks = get_averages();

    # PRETTY PRINTING DICTIONARY USING json MODULE
    print(json.dumps(student_marks, indent=4));

"Выход1

(py3.6) H:\RishikeshAgrawani\Projects\Sof\EntrySof>python EntrySof.py

(1) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> student1 56 67 35 45

(2) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> student2 67 45  35 24

(3) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> student3 101 56

ERROR: ENTERED SCORES ARE NOT IN RANGE

(3) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> student3 100 0 90

(4) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> student4 ok new

ERROR OCCURED:  invalid literal for int() with base 10: 'ok'

PLEASE ENTER CORRECT VALUE

(4) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> student4 70 40 50 70

(5) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> exit
{
    "student1": {
        "scores": [
            56,
            67,
            35,
            45
        ],
        "average": 50.75
    },
    "student2": {
        "scores": [
            67,
            45,
            35,
            24
        ],
        "average": 42.75
    },
    "student3": {
        "scores": [
            100,
            0,
            90
        ],
        "average": 63.333333333333336
    },
    "student4": {
        "scores": [
            70,
            40,
            50,
            70
        ],
        "average": 57.5
    }
}

" Выход2

(py3.6) H:\RishikeshAgrawani\Projects\Sof\EntrySof>python EntrySof.py

(1) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> hygull 50 60 70

(2) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> gurudev 78 22 50 55 45

(3) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> user1 60 -5 9

ERROR: ENTERED SCORES ARE NOT IN RANGE

(3) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> user2 100 40 kite

ERROR OCCURED:  invalid literal for int() with base 10: 'kite'

PLEASE ENTER CORRECT VALUE

(3) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> user3 90 90 100 50

(4) ENTER NAME SEPARATED BY 1 OR MORE SCORES SPACE SEPARATED (TYPE exit TO STOP) >>> exit
{
    "hygull": {
        "scores": [
            50,
            60,
            70
        ],
        "average": 60.0
    },
    "gurudev": {
        "scores": [
            78,
            22,
            50,
            55,
            45
        ],
        "average": 50.0
    },
    "user3": {
        "scores": [
            90,
            90,
            100,
            50
        ],
        "average": 82.5
    }
}
0 голосов
/ 14 июня 2018

Ниже приведен мой код, и он прекрасно работал.

if __name__ == '__main__':
    n = int(input())
    if n in range(2,11):
        student_marks = {}
        for _ in range(n):
            line = raw_input().split()
            name = line[0]
            scores =  line[1:]
            scores = list(map(float, scores))

            truth,x,y = 0,0,0
            y = len(scores)
            for x in scores:
                if 0<=x<=100:
                    truth = truth+1

            if(truth == y):
                student_marks[name] = scores
            else:
                print("Marks out of range")

        query_name = raw_input()

        add = 0
        m=0
        """for s in student_marks[query_name]:
            m = m+1"""
        #if x in student_marks:
        for x in student_marks:
            if x == query_name:
                y = len(student_marks[query_name])
                for g in (student_marks[query_name]):
                    add = add + g
                average = float(add/y)
                print("%.2f" % average)
0 голосов
/ 13 июня 2018

Вместо if x in range(0,101), вы должны использовать if x >= 0 and x <= 100 или, как @mata предлагает использовать if 0 <= x <= 100.

x in range(0, 101), возвращает True только тогда, когда x является целым числом и лежитот 0 до 100 (включительно).

...