如何在Python 3中修复无效语法? [英] How do I fix my invalid syntax in Python 3?
问题描述
在我的代码的底部,在类的下面,它说我在function.createtextfile()= create"text"的右边有一个无效的"text"语法.我在文本两边加上了引号,以便大家知道它在哪里说.这是我的代码
print(如果您要创建一个新的文本文件类型'create text',如果您要添加文本类型'add text',如果您想读取文件类型'read text' ')
At the bottom of my code, below the class, it says I have an invalid syntax for "text" on the right of function.createtextfile() = create "text". I put in quotation marks around text so you guys would know where it says that. This is my code
print("If you would like to create a new text file type ''create text'', If you would like to add text type ''add text'', If you would like to read your file type ''read text''")
class function:
def createtextfile():
text4 = '\nHello there'
text2 = open('c:/Users/Me/Desktop/Victor.txt','w')
text2.write(text)
text2.close
def addtext():
text3 = open('c:/Users/Me/Desktop/Victor.txt','a')
something = input('what would you like to add? ')
text3.write(something)
text3.close
def readtext():
justread = open('c:/Users/Me/Desktop/Victor.txt','r').read()
print(justread)
function.createtextfile() = create text
function.addtext() = add text
function.readtext() = read text
我尝试过的事情:
我试图将括号放在文本"的前面,但这没做任何事情.
What I have tried:
I have tried to put parentheses in front of "text" but that didn''t do anything.
推荐答案
该语法有两种错误.首先,您的文本字符串不包含在引号中.其次,您不能将函数设置为等于某个值.
参见 6.模块-Python 3.4.8文档 [ ^ ],了解如何正确定义和调用函数.
That syntax is wrong in two ways. Firstly your text strings are not enclosed in quotes. And secondly you cannot set a function equal to some value.
See 6. Modules — Python 3.4.8 documentation[^] for how to define and call functions correctly.
您的意思是否类似于
Do you mean something similar to
class function:
def createtextfile(self, text):
f = open('foo.txt','w')
f.write(text)
f.close()
def addtext(self):
f = open('foo.txt','a')
something = input('what would you like to add? ')
f.write(something)
f.close()
def readtext(self):
justread = open('foo.txt','r').read()
print(justread)
f = function()
f.createtextfile("foo\n")
f.addtext()
f.readtext()
这篇关于如何在Python 3中修复无效语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!