Hello, I'm a freshman in college this year.
I have a question in the assignment, so I'm writing a question
The priority tasks are as above.
You want to import a class file to do something that fits the menu.
[Code]
import os
import score
path_name = "c:/202144091"
full_filename = path_name + "/list.txt"
test_score = []
if not os.path.isdir(path_name):
os.mkdir(path_name)
elif os.path.isfile(full_filename):
f = open(full_filename, 'r', encoding="utf8")
while True:
scr = {}
line = f.readline().strip()
if not line:
break
data = line.split(",")
if len(data) == 4:
n = scr["name"] = data[0]
k = scr["kor"] = int(data[1])
e = scr["eng"] = int(data[2])
m = scr["mat"] = int(data[3])
test_score.append(score.Score(n, k, e, m))
print(f"Previously enrolled students are {len(test_score)}It's a name.")
while True:
print ("1. Input")
print ("2. Search for")
print ("3. Full Output")
print ("4. Re-read from File")
print ("0. Exit")
menu = input ("Select the desired action").strip()
if menu == "1":
while True:
f = open(full_filename, 'a', encoding="utf8")
print("Please enter your name, Korean score, English score, and math score.")
n = name = input ("Enter a name. (Exit: Baro Enter : )")
if name == "" :
break
k = kor = input ("Please enter your Korean score")
e = eng = input ("Enter English Score")
m = mat = input ("Enter your math score")
f.write(f"{n}, {k}, {e}, {m}\n")
test_score.append(score.Score(n, k, e, m))
elif menu == "2":
name = input ("Search with the name you want." (Exit if blank)")
for i in range(len(test_score)):
if name in test_score[i].name:
print(f"{test_score[i]}")
else:
Missing print(f"{name} information.")
elif menu == "3":
print(f"Currently enrolled student {len(test_score)}Name information.")
for s in test_score:
print(s)
elif menu == "4":
pass
elif menu == "0":
print ("Exit the program")
break
f.close()
However, if you search in function 2 while doing it, the following problems occur.
No matter how much I think about it, I think it's right to use for, but I'm asking you a solution
class score:
def __init__(self, name, kor):
self.name = name
self.kor = kor
def __str__(self):
return f"{self.name}, {self.kor}"
data = ["Howoni", 88] ["Lee Chanho", 30]
scores = []
for i in data:
q = score(i[0], i[1])
scores.append(q)
# # menu == "2":
name = input('input> ')
q = 0
for i in scores:
if name in i.name:
print(i)
q = 1
if q == 0:
print("Not Found")
'''
input> haha
Not Found
input> Lee Chanho
Lee Chanho, 30
'''
elif menu == "2":
name = input ("Search with the name you want." (Exit if blank)")
for i in range(len(test_score)):
if name in test_score[i].name:
print(f"{test_score[i]}")
else:
Missing print(f"{name} information.")
If you do so above, the list contains three items, and if the name of the first item matches the name you entered, the code that runs will be as follows.
print(f"{test_score[i]}")
print(f"{name} information missing.")
print(f"{name} information missing.")
Therefore, instead of putting the output in the for statement, you can first find the index of the item that matches whether there is a matching item inside the for statement, and then change it to output it only once outside the for statement.
© 2024 OneMinuteCode. All rights reserved.