我最近一直在玩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()
答案 0 :(得分:1)
您忘了调用您定义的功能。 pass
也可能导致您的函数中的语句被忽略,请删除pass
。
重新格式化代码:
#Python
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()
def write():
# pass
text_file = open('WrittenTXT.txt', "w")
type_user = input('Type: ')
text_file.write(type_user)
text_file.close()
choice = input('Append Or Write?')
if choice == "write":
write()
if choice == "append":
append()