2014-12-20 3 views
-6

이 코드에서는 사용자가 텍스트 파일을 어떤 방식으로 원하는지 묻습니다. 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 문없이 동일한 문제와 모든 작업이있다. 그게 어떤 용도라면.

+1

문자열에 대해 int를 검사하기 때문에 '1! ='1 ''입니다. – davidism

+0

이렇게하면이 프로그램을 수정할 수 있습니다. – rupton

+1

같은 유형의 개체 비교. – davidism

답변

2

way은 정수이며 1 == '1' 인 문자열을 확인하고 있습니다. 거짓입니다.

은 당신이해야 할 것은 쓰기입니다 -

if way == 1: 

하거나 입력을하고있는 int() 캐스트를 제거한 다음, 당신은 : way == '1'way == 1

way = input("Message...") 
if way == '1': 
+0

어떻게 수정해야합니까? – rupton

+0

많이 고마워요 !!!! – rupton

+0

이것은 자바 스크립트가 아닙니다. – trinalbadger587

0

변경을하므로 int에 대해 int를 검사하고 있습니다.

관련 문제