我的代码有语法错误,我不知道问题出在哪里?

I have syntax error on my code and I don't know what the problem is?

问:“询问用户他们要输入多少个数字。让他们输入这么多数字并将它们写入文本文件。每个数字必须在单独的一行上。”我不知道错误是什么

user = int(input("how many numbers to enter"))
file = open("file1.txt" , "a")
for x in range(user):
    number = input("Enter number" + str(user + 1) + "\n")
    file.writelines(user+"\n")  
file.close()

我相信您使用的是 user 变量而不是 x 变量。固定码:

user = int(input("how many numbers to enter"))
with open("file1.txt" , "a") as file:
    for x in range(user):
        number = input("Enter number " + str(x + 1) + "\n")
        file.writelines(user + "\n")  

通过阅读练习顺序,我发现您已经接近答案并假设这就是您想要的。

user = int(input("How many numbers you want to enter?\n"))
file = open("file1.txt", "w")
for x in range(user):
    number = input("Enter number " + str(x + 1) + ":\n")
    file.writelines(str(x) + "\n")
file.close()

您忘记添加用户变量并在行

file.writelines(user+"\n")  

您忘记将 int 转换为字符串。

file.writelines(str(user)+"\n")

请注意,您的代码只是将用户编号写入“user”次。

这是经过编辑的适合我的代码:

user = 5 #number of users
file = open("file1.txt" , "a")
for x in range(user): #loop every user
    number = input("Enter number" + str(x + 1) + "\n") #Use "str()"" to convert number, called int, to a string 
    file.writelines(str(user) + "\n") 
file.close()

尽管我不清楚你的问题,但我想这就是你所期望的。

user_input = int(input("how many numbers to enter:"))
with open("file1.txt" , "w") as output_file:
    for x in range(user_input):
        number = input("Enter number" + f'{x + 1}:' + "\n")
        print(number, file=output_file)

因为print函数本身做换行,你可以省略添加新行。