The entered variable will appear once more.

Asked 2 years ago, Updated 2 years ago, 91 views

a=(10, 'Hong')
b=(11), 'im')
c=(12), 'HAN'
dic = dict((a, b, c))
print ('Student Information:', dic)
while True:
 x=int (input('Enter academic year:'))
 if x < 0:
  print ("End Program")
  break
 elif x in dic:
  print(x, 'Burn student', dic[x])
  x=int (input('Enter academic year:'))
 else:
  print ('No students in this class')
  x=int (input('Enter academic year:'))

If you enter the code like this, you will see the following. You have to keep typing to get it as you originally thought. What's the problem?

<Results>

Student information: {191101: 'Hong Gil-dong', 191102: 'Im Kkeok-jeong', 191103: 'Jang Gil-san'}

Enter school number: 191101

Student number 191101 is Gildong Hong.

Please enter your school number:

Please enter your school number:

There are no students in this class.

Enter school number: -1

Enter school number: -1

Exit the program.

python dictionary errorcode

2022-09-20 17:05

1 Answers

Because it is a WHILE loop statement, the X value will continue to be entered unless it is BREAKED. If so, there is no reason to receive an additional X from ELIF or ELSE.

a=(10, 'Hong')
b=(11), 'im')
c=(12), 'HAN'
dic = dict((a, b, c))
print ('Student Information:', dic)
while True:
 x=int (input('Enter academic year:'))
 if x < 0:
  print ("End Program")
  break
 elif x in dic:
  print(x, 'Burn student', dic[x])
  #x=int (input('Enter academic year:'))
 else:
  print ('No students in this class')
  #x=int (input('Enter academic year:'))


2022-09-20 17:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.