테스트 사이트 - 개발 중인 베타 버전입니다

파이썬 질문

basdr 5년 전 조회 3,665

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

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

 

리스트를 만들고 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개

화랑백
5년 전

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

</p>

<pre>
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)</pre>

<p>

 

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

</p>

<pre>
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)</pre>

<p>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인