Sorting a dictionary by key using the import Library returns sorted values only for the first digit [closed]









up vote
0
down vote

favorite












Program Description:



  • Read The student's name and marks and store it in a dictionary

  • Rank the Students by the Dictionary value

  • Give cash rewards to top 3 students based on rank

  • write appreciation to the students who have scored more than 950 marks

enter image description here



I have used the operator library to sort my dictionary for Students Marks record Program but as I am sorting the dictionary based on the marks, It seems to be sorting the dictionary only based on the first digit (from left to right) as shown below:



enter image description here



For example if the marks provided is 700,80,9 it sorts the marks as 9,80,700.
I am guessing the code is just checking for the first digit and if it is found to be greater than the first digit of another number (though second number it is greater than the first number), the first number is only shown to be greater.










share|improve this question















closed as off-topic by jpp, chepner, stovfl, Rob, Jaba Nov 10 at 2:43


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jpp, chepner, Rob, Jaba
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Next time, copy-paste your code and error message (if any) as text.
    – Austin
    Nov 9 at 16:44






  • 3




    @Austin What do you mean, "next time"? The current question can and should be fixed.
    – chepner
    Nov 9 at 16:49











  • Do not post images of code or errors! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly.
    – Rob
    Nov 10 at 2:05














up vote
0
down vote

favorite












Program Description:



  • Read The student's name and marks and store it in a dictionary

  • Rank the Students by the Dictionary value

  • Give cash rewards to top 3 students based on rank

  • write appreciation to the students who have scored more than 950 marks

enter image description here



I have used the operator library to sort my dictionary for Students Marks record Program but as I am sorting the dictionary based on the marks, It seems to be sorting the dictionary only based on the first digit (from left to right) as shown below:



enter image description here



For example if the marks provided is 700,80,9 it sorts the marks as 9,80,700.
I am guessing the code is just checking for the first digit and if it is found to be greater than the first digit of another number (though second number it is greater than the first number), the first number is only shown to be greater.










share|improve this question















closed as off-topic by jpp, chepner, stovfl, Rob, Jaba Nov 10 at 2:43


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jpp, chepner, Rob, Jaba
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Next time, copy-paste your code and error message (if any) as text.
    – Austin
    Nov 9 at 16:44






  • 3




    @Austin What do you mean, "next time"? The current question can and should be fixed.
    – chepner
    Nov 9 at 16:49











  • Do not post images of code or errors! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly.
    – Rob
    Nov 10 at 2:05












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Program Description:



  • Read The student's name and marks and store it in a dictionary

  • Rank the Students by the Dictionary value

  • Give cash rewards to top 3 students based on rank

  • write appreciation to the students who have scored more than 950 marks

enter image description here



I have used the operator library to sort my dictionary for Students Marks record Program but as I am sorting the dictionary based on the marks, It seems to be sorting the dictionary only based on the first digit (from left to right) as shown below:



enter image description here



For example if the marks provided is 700,80,9 it sorts the marks as 9,80,700.
I am guessing the code is just checking for the first digit and if it is found to be greater than the first digit of another number (though second number it is greater than the first number), the first number is only shown to be greater.










share|improve this question















Program Description:



  • Read The student's name and marks and store it in a dictionary

  • Rank the Students by the Dictionary value

  • Give cash rewards to top 3 students based on rank

  • write appreciation to the students who have scored more than 950 marks

enter image description here



I have used the operator library to sort my dictionary for Students Marks record Program but as I am sorting the dictionary based on the marks, It seems to be sorting the dictionary only based on the first digit (from left to right) as shown below:



enter image description here



For example if the marks provided is 700,80,9 it sorts the marks as 9,80,700.
I am guessing the code is just checking for the first digit and if it is found to be greater than the first digit of another number (though second number it is greater than the first number), the first number is only shown to be greater.







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 2:18









Rob

11.4k82852




11.4k82852










asked Nov 9 at 16:27









asiffarhankhan

196




196




closed as off-topic by jpp, chepner, stovfl, Rob, Jaba Nov 10 at 2:43


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jpp, chepner, Rob, Jaba
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by jpp, chepner, stovfl, Rob, Jaba Nov 10 at 2:43


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jpp, chepner, Rob, Jaba
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 1




    Next time, copy-paste your code and error message (if any) as text.
    – Austin
    Nov 9 at 16:44






  • 3




    @Austin What do you mean, "next time"? The current question can and should be fixed.
    – chepner
    Nov 9 at 16:49











  • Do not post images of code or errors! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly.
    – Rob
    Nov 10 at 2:05












  • 1




    Next time, copy-paste your code and error message (if any) as text.
    – Austin
    Nov 9 at 16:44






  • 3




    @Austin What do you mean, "next time"? The current question can and should be fixed.
    – chepner
    Nov 9 at 16:49











  • Do not post images of code or errors! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly.
    – Rob
    Nov 10 at 2:05







1




1




Next time, copy-paste your code and error message (if any) as text.
– Austin
Nov 9 at 16:44




Next time, copy-paste your code and error message (if any) as text.
– Austin
Nov 9 at 16:44




3




3




@Austin What do you mean, "next time"? The current question can and should be fixed.
– chepner
Nov 9 at 16:49





@Austin What do you mean, "next time"? The current question can and should be fixed.
– chepner
Nov 9 at 16:49













Do not post images of code or errors! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly.
– Rob
Nov 10 at 2:05




Do not post images of code or errors! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly.
– Rob
Nov 10 at 2:05












3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










You are inputting the number as string. As you can see the dict shows the numbers in quotes. You have to get the number input as wither int or float.



value = int(input("enter the value: "))





share|improve this answer



























    up vote
    2
    down vote













    You're sorting based on string values, not integer values. You need to convert your marks to integers. When sorting by integer, 855 > 9. When sorting as though they were strings, this isn't the case.






    share|improve this answer



























      up vote
      0
      down vote













      First, dont upload screenshots of your code. Use the code sample icon on the editor to post the actual code. Also include just the function that has the problem - rank.



      All the following code is at the top of your rank function.



      First, convert the numeric element of each tuple to an int:



      students2 = [(a,int(b)) for (a,b) in student]


      Then replace ranked = sorted(students.items(), key=operator.itemgetter(1),reverse=True)



      with sorted(student2, key=lambda x:x[1], reverse=True)



      As a bonus, no need for the operator dependency.






      share|improve this answer





























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote



        accepted










        You are inputting the number as string. As you can see the dict shows the numbers in quotes. You have to get the number input as wither int or float.



        value = int(input("enter the value: "))





        share|improve this answer
























          up vote
          4
          down vote



          accepted










          You are inputting the number as string. As you can see the dict shows the numbers in quotes. You have to get the number input as wither int or float.



          value = int(input("enter the value: "))





          share|improve this answer






















            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            You are inputting the number as string. As you can see the dict shows the numbers in quotes. You have to get the number input as wither int or float.



            value = int(input("enter the value: "))





            share|improve this answer












            You are inputting the number as string. As you can see the dict shows the numbers in quotes. You have to get the number input as wither int or float.



            value = int(input("enter the value: "))






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 9 at 16:31









            Naveen

            531112




            531112






















                up vote
                2
                down vote













                You're sorting based on string values, not integer values. You need to convert your marks to integers. When sorting by integer, 855 > 9. When sorting as though they were strings, this isn't the case.






                share|improve this answer
























                  up vote
                  2
                  down vote













                  You're sorting based on string values, not integer values. You need to convert your marks to integers. When sorting by integer, 855 > 9. When sorting as though they were strings, this isn't the case.






                  share|improve this answer






















                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    You're sorting based on string values, not integer values. You need to convert your marks to integers. When sorting by integer, 855 > 9. When sorting as though they were strings, this isn't the case.






                    share|improve this answer












                    You're sorting based on string values, not integer values. You need to convert your marks to integers. When sorting by integer, 855 > 9. When sorting as though they were strings, this isn't the case.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 16:33









                    John Rouhana

                    1388




                    1388




















                        up vote
                        0
                        down vote













                        First, dont upload screenshots of your code. Use the code sample icon on the editor to post the actual code. Also include just the function that has the problem - rank.



                        All the following code is at the top of your rank function.



                        First, convert the numeric element of each tuple to an int:



                        students2 = [(a,int(b)) for (a,b) in student]


                        Then replace ranked = sorted(students.items(), key=operator.itemgetter(1),reverse=True)



                        with sorted(student2, key=lambda x:x[1], reverse=True)



                        As a bonus, no need for the operator dependency.






                        share|improve this answer


























                          up vote
                          0
                          down vote













                          First, dont upload screenshots of your code. Use the code sample icon on the editor to post the actual code. Also include just the function that has the problem - rank.



                          All the following code is at the top of your rank function.



                          First, convert the numeric element of each tuple to an int:



                          students2 = [(a,int(b)) for (a,b) in student]


                          Then replace ranked = sorted(students.items(), key=operator.itemgetter(1),reverse=True)



                          with sorted(student2, key=lambda x:x[1], reverse=True)



                          As a bonus, no need for the operator dependency.






                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            First, dont upload screenshots of your code. Use the code sample icon on the editor to post the actual code. Also include just the function that has the problem - rank.



                            All the following code is at the top of your rank function.



                            First, convert the numeric element of each tuple to an int:



                            students2 = [(a,int(b)) for (a,b) in student]


                            Then replace ranked = sorted(students.items(), key=operator.itemgetter(1),reverse=True)



                            with sorted(student2, key=lambda x:x[1], reverse=True)



                            As a bonus, no need for the operator dependency.






                            share|improve this answer














                            First, dont upload screenshots of your code. Use the code sample icon on the editor to post the actual code. Also include just the function that has the problem - rank.



                            All the following code is at the top of your rank function.



                            First, convert the numeric element of each tuple to an int:



                            students2 = [(a,int(b)) for (a,b) in student]


                            Then replace ranked = sorted(students.items(), key=operator.itemgetter(1),reverse=True)



                            with sorted(student2, key=lambda x:x[1], reverse=True)



                            As a bonus, no need for the operator dependency.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 9 at 19:48

























                            answered Nov 9 at 16:44









                            Nick

                            6191830




                            6191830













                                Popular posts from this blog

                                Kleinkühnau

                                Makov (Slowakei)

                                Deutsches Schauspielhaus