Python说:“预计会有一个缩进的块”

时间:2016-12-02 21:08:24

标签: python shell cmd stack indentation

我最近一直在玩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()

1 个答案:

答案 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()