파이썬 질문

파이썬 질문

QA

파이썬 질문

답변 1

본문

파이썬 공부하고 있는 초보 학생입니다.

오류는 아니고 제가 원하는데로 실행이 안되는데 이유를 모르겠습니다.

 

리스트를 만들고 4명의 학생의 정보를 입력받아 이중 리스트로 저장하려는데

값은 잘 들어가는거 같은데 이중 리스트에 값을 넣으면 마지막 학생값으로 다 바뀌어 져 있어습니다.

 

name= ""
kor=0
eng=0
math=0
plus=0
avg=0
gardes=""

student=[name,kor,eng,math,plus,avg,gardes]


student1=student
student1[0]=str(input('성명: '))
student1[1]=int(input('국어점수: '))
student1[2]=int(input('영어점수: '))
student1[3]=int(input('수학점수: '))
print(student1)
student2=student
student2[0]=str(input('성명: '))
student2[1]=int(input('국어점수: '))
student2[2]=int(input('영어점수: '))
student2[3]=int(input('수학점수: '))
print(student2)
student3=student
student3[0]=str(input('성명: '))
student3[1]=int(input('국어점수: '))
student3[2]=int(input('영어점수: '))
student3[3]=int(input('수학점수: '))
print(student3)
student4=student
student4[0]=str(input('성명: '))
student4[1]=int(input('국어점수: '))
student4[2]=int(input('영어점수: '))
student4[3]=int(input('수학점수: '))
print(student4)

 

ban=[student1,student2,student3,student4]


print(ban)

 

결과

성명: 학생1

국어점수: 90

영어점수: 80

수학점수: 70

['학생1', 90, 80, 70, 0, 0, '']

성명: 학생2

국어점수: 84

영어점수: 93

수학점수: 78

['학생2', 84, 93, 78, 0, 0, '']

성명: 학생3

국어점수: 79

영어점수: 82

수학점수: 80

['학생3', 79, 82, 80, 0, 0, '']

성명: 학생4

국어점수: 90

영어점수: 91

수학점수: 69

['학생4', 90, 91, 69, 0 ,0, '']

[['학생4', 90, 91, 69, 0 ,0, ''], ['학생4', 90, 91, 69, 0 ,0, ''], ['학생4', 90, 91, 69, 0 ,0, ''], ['학생4', 90, 91, 69, 0 ,0, '']]

 

이 질문에 댓글 쓰기 :

답변 1

student1,2,3,4들이 student와 변수가 공유되면서 값이 함께 바뀌고 있어요 아래와 같이 확인해보실수 있어요



name = ""
kor = 0
eng = 0
math = 0
plus = 0
avg = 0
gardes = ""

student = [name, kor, eng, math, plus, avg, gardes]

student1 = student
student1[0] = str(input('성명: '))
student1[1] = int(input('국어점수: '))
student1[2] = int(input('영어점수: '))
student1[3] = int(input('수학점수: '))
print(student1)
student2 = student
student2[0] = str(input('성명: '))
student2[1] = int(input('국어점수: '))
student2[2] = int(input('영어점수: '))
student2[3] = int(input('수학점수: '))
print(student1)
print(student2)
student3 = student
student3[0] = str(input('성명: '))
student3[1] = int(input('국어점수: '))
student3[2] = int(input('영어점수: '))
student3[3] = int(input('수학점수: '))
print(student1)
print(student2)
print(student3)
student4 = student
student4[0] = str(input('성명: '))
student4[1] = int(input('국어점수: '))
student4[2] = int(input('영어점수: '))
student4[3] = int(input('수학점수: '))
print(student1)
print(student2)
print(student3)
print(student4)


ban = [student1, student2, student3, student4]

print(ban)

 

그래서 해결법으로는 변수들 공유를 안하게 해주면 되요



name = ""
kor = 0
eng = 0
math = 0
plus = 0
avg = 0
gardes = ""

student = [name, kor, eng, math, plus, avg, gardes]

student1 = [name, kor, eng, math, plus, avg, gardes]
student1[0] = str(input('성명: '))
student1[1] = int(input('국어점수: '))
student1[2] = int(input('영어점수: '))
student1[3] = int(input('수학점수: '))
print(student1)
student2 = [name, kor, eng, math, plus, avg, gardes]
student2[0] = str(input('성명: '))
student2[1] = int(input('국어점수: '))
student2[2] = int(input('영어점수: '))
student2[3] = int(input('수학점수: '))
print(student1)
print(student2)
student3 = [name, kor, eng, math, plus, avg, gardes]
student3[0] = str(input('성명: '))
student3[1] = int(input('국어점수: '))
student3[2] = int(input('영어점수: '))
student3[3] = int(input('수학점수: '))
print(student1)
print(student2)
print(student3)
student4 = [name, kor, eng, math, plus, avg, gardes]
student4[0] = str(input('성명: '))
student4[1] = int(input('국어점수: '))
student4[2] = int(input('영어점수: '))
student4[3] = int(input('수학점수: '))
print(student1)
print(student2)
print(student3)
print(student4)


ban = [student1, student2, student3, student4]

print(ban)

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 125,873
© SIRSOFT
현재 페이지 제일 처음으로