+ 2

Please suggest any modification to this code

So I was recently working on a student database management system. However, I'm having two problems with the code I have written: 1. I'm not able to extract the percentage from d.values() 2. When I enter the choice a third time, the code stops working Please ignore the fact that I didn't put the codes for if(x==4) and if(x==5) import collections def studentmanagement(): print("[Choice 1: Accept details of the students ]\n") print("[Choice 2: Search details of a particular student on the basis of allottee number and display result ]\n") print("[Choice 3: Display the result of all the students ]\n") print("[Choice 4: Find the topper(s) amongst the students ]\n") print("[Choice 5: Find the grade of the students ]\n") print("[Choice 6: Exit ]\n") x = int(input("Enter a choice: ")) if(x==1): n=int(input("Enter the number of students(n):")) print("Enter the details of the students in the following format:allottee no. name, percentage:") d = dict(input().split(' ') for i in range(n)) print("Details accepted successfully!") x = int(input("Enter a choice: ")) if(x==2): y=input("Enter the allottee number of the student:") if y in d.keys(): print(d.get(y)) x = int(input("Enter a choice: ")) else: print("This allottee number is not present in the database") x = int(input("Enter a choice: ")) if(x==3): print("Raw database:") print(d) l=d.values() res = [(a, b) for a, b in zip(*d.values()) if a.isdigit()] # printing result print("The Percentages scored by the students : " + str(res)) x = int(input("Enter a choice: ")) if(x==6): print("Thank you") studentmanagement()

13th May 2021, 9:07 AM
MsBonnie
MsBonnie - avatar
6 ответов
+ 3
Luan Thierry You might find the approach I took on line 65 in my code to be an interesting alternative to your recommendation.
15th May 2021, 6:32 AM
David Carroll
David Carroll - avatar
+ 3
MsBonnie I just updated the code quite a bit with much cleaner organization using classes. https://code.sololearn.com/cYAf37NVX3p8/?ref=app I hope you see these responses. Otherwise, putting any amount of time into this would have been a huge waste of time.
16th May 2021, 6:58 AM
David Carroll
David Carroll - avatar
+ 2
MsBonnie I messed around a bit with refining your code. Take a look, let me know if you have any questions. https://code.sololearn.com/cYAf37NVX3p8/?ref=app
15th May 2021, 5:16 AM
David Carroll
David Carroll - avatar
+ 2
CarrieForle Generally speaking your advice is great! However, you might be surprised to learn that Python doesn't support block (or nested) scoped variables. An alternative is to use nested functions in which local scope applies to new named variables in the inner function. This essentially sets up a closure. However, Python closures are a bit limited since the outer scoped variables are readonly from within the inner scope. TBH... Python scopes, in general, are a bit of a mess for developers coming from other languages.
15th May 2021, 6:23 AM
David Carroll
David Carroll - avatar
+ 1
Haven't tested yet, but looks like all the variables you declared are scoped to the if statements respectively. Such as d which you declared in if(x==1) scope and you try to access it from if(x==3). That wouldn't work. Try to declare variables that need to be accessed across before the if statement.
13th May 2021, 10:44 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
sorry to intrude on your code but i would take the prints and put it like this: one = ("[Choice 1: Accept details of the students ]") two = ("[Choice 2: Search details of a particular student on the basis of allottee number and display result ]") three = ("[Choice 3: Display the result of all the students ]") four = ("[Choice 4: Find the topper(s) amongst the students ]") five = ("[Choice 5: Find the grade of the students ]") six = ("[Choice 6: Exit ]") a = input(f'{one}\n{two}\n{three}\n{four}\n{five}\n{six}\n')
13th May 2021, 6:30 PM
Luan Thierry
Luan Thierry - avatar