이 코드는 절대적으로, 100 % 정확합니다. 그것은 교사의 컴퓨터에서 작동하고 완벽하게 실행되지만 내 것은 그렇지 않습니다. 나는 그것이 왜 될지 전혀 모른다. 이 문제는 Mac 및 Windows에서 동일합니다 (다른 사람도 동일한 문제가 있음). 필자는 관련이 있다면 Windows 8.1을 사용하고 모든 Python 파일을 Documents에 저장합니다. 나는 파이썬 3.5를 가지고 있고 쉘과 명령 창에서 같은 문제가있다.코드가 정확하고 교사가 승인했지만 작동하지 않습니다. 어떻게 실행시킬 수 있습니까? (파이썬 무작위 모듈)
정상적으로 작동합니다 - 예상되는 학생 이름을 인쇄하지만 끝 메시지를 두 번 인쇄합니다.
이전에 거북이 모듈을 0 개 가져 왔습니다. 나는 그것과 무작위 이외의 아무것도 가져 오지 않았으므로, 그것은 단지 무작위인지 또는 다른 모듈 들인지 확실하지 않다.
다음은 코드입니다. 학생들에게 무작위로 활동을 선택하는 것입니다.
def addlist():
maxLengthList = int(input("How many students?"))
turtle.title("Class Chooser")
while len(kids) < maxLengthList:
name = turtle.textinput("Students", "Enter a name")
kids.append(name)
nextturn()
def nextturn():
turn = random.choice(kids)
kids.remove(turn)
print("Next " + turn)
again=len(kids)
while again>0:
nextturn()
again=again-1
else:
print("Have a nice day!")
import random
import turtle
kids = []
addlist()
다음은 오류 메시지입니다. 당신의 모든이에 대한 수정이
How many students?4
Next lucy
Next alex
Next jake
Next bro
Have a nice day!
Have a nice day!
Traceback (most recent call last):
File "C:\Users\Seren\AppData\Local\Programs\Python\Python35-32\lib\random.py", line 253, in choice
i = self._randbelow(len(seq))
File "C:\Users\Seren\AppData\Local\Programs\Python\Python35-32\lib\random.py", line 230, in _randbelow
r = getrandbits(k) # 0 <= r < 2**k
ValueError: number of bits must be greater than zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Seren\Documents\~School~\Computer Programming\isolated code ex.py", line 24, in <module>
addlist()
File "C:\Users\Seren\Documents\~School~\Computer Programming\isolated code ex.py", line 7, in addlist
nextturn()
File "C:\Users\Seren\Documents\~School~\Computer Programming\isolated code ex.py", line 16, in nextturn
nextturn()
File "C:\Users\Seren\Documents\~School~\Computer Programming\isolated code ex.py", line 16, in nextturn
nextturn()
File "C:\Users\Seren\Documents\~School~\Computer Programming\isolated code ex.py", line 10, in nextturn
turn = random.choice(kids)
File "C:\Users\Seren\AppData\Local\Programs\Python\Python35-32\lib\random.py", line 255, in choice
raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence
가 수행 나는 학생들의 이름 루시, 알렉스, 제이크, 그리고 형제를 입력 한? 제대로 무작위로 가져올 수 없다면 과제를 수행하는 것이 거의 불가능합니다.
편집 : 제 컴퓨터 나 운영체제, 또는 내가 다운로드 한 파이썬 버전에 문제가 있다고 생각합니다. 무작위 모듈을 이상하게 만드는 코드 외부의 모든 것.
업데이트 : print ("좋은 하루 되세요!") 후에 quit()을 넣으면 오류 메시지가 나타나지 않고 프로그램이 의도 한대로 작동하는 것으로 보입니다. 비록 이것이 이것이 band-aid만큼 많은 해결책인지는 모르겠다.
귀하의 코드가 정의되기 전에 호출되는 바닥 함수에서 수입으로, 뒤쪽으로 보인다. – TigerhawkT3
을 당신은 nextturn'에 대한 호출을() '(정의되기 전에) 작동하지 않아야하지만,'addlist'를 호출하기 전에'nextturn'을 호출하면 분명히이 에러가 발생합니다. – chepner
사람들이 이상하게 보일지 모르지만 문제는'nextturn'이 아닙니다. 그 코드가 정의되기 전에 불려지는 것입니다 (어쩌면 그가 여기에 코드를 썼을 때 단지 아이덴티티 에러 였을 것입니다). 에러는 'random.choice (kids)'가 호출 될 때'kids'가 빈 시퀀스라는 것입니다. – lucasnadalutti