【发布时间】:2015-02-19 08:17:32
【问题描述】:
在这段代码中,我询问用户他们希望文本文件以哪种方式排序,但是,运行时 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 语句的情况下都可以工作它们都使用 if 语句没有 elif ect。如果这有任何用处。
【问题讨论】:
标签: python if-statement