:このPythonのリストコードで何が問題になっていますか?私はこのコードについて質問してい
menu = ""
student = []
print ("What would you like to do?")
print ("\t[1] Show full gradebook")
print ("\t[2] Add Student")
print ("\t[3] Remove Student")
print ("\t[4] Modify Student Information")
print ("\t[5] Display Highest Grade")
print ("\t[6] Display Lowest Grade")
print ("\t[7] Quit")
# loop until the user decides to quit
while menu != 7:
menu = int(input("Enter selection [1-7]"))
if menu == 1:
print("Name\tGrade")
# loop through all students
for s in student:
print(s[0]+"\t"+str(s[1]))
elif menu == 2:
# prompt user for student name
sname = input("Student Name?")
# prompt user for student grade
sgrade = int(input("Student Grade?"))
# append student information to list
student.append([sname, sgrade])
elif menu == 3:
sname = input("Student to remove?")
try:
student.remove([sname, sgrade])
except:
if sname not in student:
print("Not in table.")
elif menu == 4:
sname = input("Student to modify?")
for s in student:
print(s[0]+"\t"+str(s[1]))
try:
student.remove([sname, sgrade])
sname = input("Name: (press Enter to keep original value)")
sgrade = int(input("Grade: (press Enter to keep original value)"))
student.append([sname, sgrade])
except:
if sname not in student:
print("Not in table.")
elif menu == 5:
try:
print(sname + " had the highest score in the class: " + str(sgrade))
except:
pass
elif menu == 6:
try:
print(sname + " had the lowest score in the class: " + str(sgrade))
except:
pass
elif menu >= 8:
print("Invalid selection.")
print ("Terminating program... Goodbye!")
私は、複数の学生と〜6の3つのオプションを試すたびに、それは、リストの一番下にある学生のためにそれを行います。また、生徒の情報を変更しているときに、ある生徒の元の名前や学年を維持する方法を知りたい。
は「それを行う」 - 何をしますか? – DyZ
各アクションを別々の機能に移動すると、問題が表示されます。また、whileループをメイン関数に移動します。以前の繰り返しから 'sgrade'変数を再利用しています – balki