Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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'")
Python
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.
Posted
Updated 13-Mar-23 4:21am
v2

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.
 
Share this answer
 
Do you mean something similar to
Python
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()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900