sudhir kumar
sudhir kumar

Reputation: 115

python function parameters that include symbols

I wrote a python script as below

def single_value(account,key):

        file = open('%s.txt'%account)
        file.write('Hello')
        file.close()
        file2 =  open('%s.txt'%key)
        file2.write('hoiiii')
        file2.close()
single_value(accountname, 2345kwjhf53825==)

when I execute the script I am getting error invalid syntax. I think it is because of '==' in key. Is there is a way to define this key. Please help

Upvotes: 0

Views: 205

Answers (1)

John1024
John1024

Reputation: 113834

The invalid syntax error is because strings must be in quotes. Thus, replace:

single_value(accountname, 2345kwjhf53825==)

With:

single_value('accountname', '2345kwjhf53825==')

The next error is that the files are opened read-only and you want to write to them. All together:

def single_value(account,key):

    with open('%s.txt'%account, 'w') as file:
        file.write('Hello')
    with open('%s.txt'%key, 'w') as file2:
        file2.write('hoiiii')

single_value('accountname', '2345kwjhf53825==')

Upvotes: 2

Related Questions