이 코드에서는 사용자가 텍스트 파일을 어떤 방식으로 원하는지 묻습니다. if 문을 실행하면 작동하지 않으며 if 문없이 실행하면 작동합니다. 왜 작동하지 않을까요? 내가 표시하지 않은이 경우 if 문이 작동하지 않는 이유는 무엇입니까?
way = int(input('Which way would you like the data to be sorted, Alphabetical[1], Ascending[2] Descending[3] or Average[4]'))
classno = str(input('Which class would you like to sort? Please state the entire class name no spaces please with .txt on the end'))
if way=='1':# WORKS with classno but not with if statement
f = open(classno, "r")# omit empty lines and lines containing only whitespace
lines = [line for line in f if line.strip()]
f.close()
lines.sort()# now write the output file
f = open(classno, 'w')
f.writelines(lines) # Write a sequence of strings to a file
f.close()
다른 코드는 그들은 모두 if 문없이 ELIF 요법을 사용하는 if 문없이 동일한 문제와 모든 작업이있다. 그게 어떤 용도라면.
문자열에 대해 int를 검사하기 때문에 '1! ='1 ''입니다. – davidism
이렇게하면이 프로그램을 수정할 수 있습니다. – rupton
같은 유형의 개체 비교. – davidism