'def'/myName = input() \r
/myName = 'Albert' \r
text_file = open("C:\Output.txt", "w") \r
text_file.write("myName") \r
text_file.close()
我的意思是,这是我第一次与Python编码的作业,但它不断给我这个“语法错误:意想不到的字符后续行字符“在第一个\ r之后。有人可以解释我/请帮助我吗?
-Donny
'def'/myName = input() \r
/myName = 'Albert' \r
text_file = open("C:\Output.txt", "w") \r
text_file.write("myName") \r
text_file.close()
我的意思是,这是我第一次与Python编码的作业,但它不断给我这个“语法错误:意想不到的字符后续行字符“在第一个\ r之后。有人可以解释我/请帮助我吗?
-Donny
myName = raw_input('type name: ')
with open("output.txt", "w") as textfile:
textfile.write(myName)
尽管此代码可能会回答问题,但提供关于此代码为何和/或如何回答问题的其他上下文可提高其长期价值。 – JAL
你的代码表明,你是不是在所有熟悉Python的语法和它是如何工作。这是你的错误的解释(按照您的要求):
'def'/myName = input() \r
高清并不需要围绕它撇号和MYNAME不需要斜杠。你不能设置一个功能做一些事情上的第一行 - 这是正确的语法:
def foo():
bar = input('carrot: ')
print(bar)
此外,你需要存储输入在做任何事情之前的变量; 输入需要一个字符串。每行后都不需要回车。在括号后面,你需要冒号。
/myName = 'Albert' \r
您不能设置函数使其等于一个字符串。
text_file = open("C:\Output.txt", "w") \r
text_file.write("myName") \r
MYNAME是一个变量,而不是字符串,所以你不需要引号。
text_file.close()
这里是工作代码:
myName = input('Name: ') # ask for input, which will be stored in myName
text_file = open('C:\output.txt', 'w') # open the output file in write mode
text_file.write(myName) # write myName to the output file
我会建议进一步尝试任何东西之前读official Python tutorial。
对你的代码做一个Ctr-K,它会正确对齐。 – LetzerWille
您需要在源代码中使用与Python解释器中预期换行符相同的换行符格式。您无需在源代码中明确输入换行符。 – shuttle87