【发布时间】:2017-04-17 20:58:53
【问题描述】:
我最近一直在玩 Python,在创建函数时遇到了这个错误。我似乎无法修复它:(。代码:
Python
#Python
choice = input('Append Or Write?')
if choice == "write":
def write():
pass
text_file = open('WrittenTXT.txt', "w")
type_user = input('Type: ')
text_file.write(type_user)
text_file.close()
if choice == "append":
def append():
# Making a txt file
#Append
pass
text_file = open('WrittenTXT.txt', "a")
user_int = input('Enter An Integer: ')
space = "\n" * 2
lines = [space, "Hi\n", "Hallo\n", "Bonjour\n", user_int]
text_file.writelines(lines)
text_file.close()
【问题讨论】:
-
这是函数的一部分吗?很难说。记住在 python 中,缩进定义了范围。您的第一行(以
choice开头)未与其正下方的行对齐。可能是您在 StackOverlow 中的格式,但我对此表示怀疑。回顾您的代码并确保同一范围内的行对齐。 -
格式错误的代码是您的问题。在提出晦涩的问题之前,我建议您阅读有关 Python 的基础知识。
标签: python shell cmd stack indentation