Home > Back-end >  How to set an integer in a function?
How to set an integer in a function?

Time:10-29

I am trying to set a passcode as an integer, in my case I am using a function.
Here is my code:

def create(user, int(password)):
  file = open('accounts.txt', 'a')
  file.write(user   ' '   password)
  file.write('\n')
  file.close()

create('Benni', 'code')

I am getting an error when defining the password variable in the function though? Any help!

CodePudding user response:

First problem - declaring int(password). Python doesn't like that, since in this case "password" is a dummy variable for the function. You could add "password = int(password)" to the body of the function (not in the variables definition!) but it's not necessary.

Also there were some indentation errors.

Hopefully this should fix some of the issues you're having:

def create(user, password):
    file = open('accounts.txt', 'a')
    file.write(user   ' '   password)
    file.write('\n')
    file.close()

CodePudding user response:

You don't call int() in the parameter list of the function definition. You call it in the argument list when calling the function.

But to concatenate it with user you have to convert it back to a string.

def create(user, password):
    with open('accounts.txt', 'a') as file:
        file.write(user   ' '   str(password))
        file.write('\n')

create('Benni', int('code'))
  • Related