'Non callable variable
I'm trying to make a password generator with Python. Everything else seems to work correctly, but when I try to save the password in a .txt file, the program crashes, telling me that ok_to_save is "non callable". Why does this happen, and how do I fix it?
PS: Don't mind option 6 I'm still experimenting with it.
import random
import string
print('Hi, would you like me to create a password that no one can guess ?')
confirmation=input()
print('If you want a password with only letters press 1 and then enter, for one with only numbers press 2 and then enter.')
print('For a password with both press 3 and then enter.')
print('For a password with letters, numbers and symbols press 4 and then enter.')
print('To remember a password you previously saved in this application press 5 and then enter.')
print('To save a password you made yourself press 6 and then enter')
a=input()
b=''
ok_to_save=''
if a=='1':
b=input = ''.join([random.choice(string.ascii_letters) for x in range(12)])
print(b)
print('Would you like me to save this password ?')
ok_to_save=input()
elif a=='2':
b=input = ''.join([random.choice(string.digits) for n in
range(12)])
print(b)
print('Would you like me to save this password ?')
ok_to_save=input()
elif a=='3':
b=input = ''.join([random.choice(string.ascii_letters + string.digits) for n in
range(12)])
print(b)
print('Would you like me to save this password ?')
ok_to_save=input()
elif a=='4':
b=input = ''.join([random.choice(string.ascii_letters + string.digits + '!' + '@' + '#' + '$' + '%' + '^' + '&' + '*' + '(' + ')') for n in range(12)])
print(b)
print('Would you like me to save this password ?')
ok_to_save=input()
elif a=='5':
f=open("database.txt","r")
f=open('database.txt','r')
print(f.read())
elif a=='6':
print('What is the password ?')
alt_b=input()
if ok_to_save=='Yes' or ok_to_save=='yes':
print('What is this password for ? ')
c=input()
f=open('database.csv','w')
password=''+b+','+c
f.write(password)
f.close()
Solution 1:[1]
Ok so from my experimenting, the issue comes in here. When you do
b=input= ''.join([random.choice(string.ascii_letters + string.digits) for n in
range(12)])
What this does is it creates a variable named 'input' and stores the result of the join method in the variable input as well as in the varible b. The reason you get an error at the ok to save line is because you're doing
ok_to_save = input()
The python interpreter sees input and is thinking that you're referring to the variable that you created above and not the function which takes input and therefore says 'str object not callable' because the str object is not a function that you can call.
As a future reference, avoid creating variables with the same name as functions. To solve your issue that you have now, wherever you have
b = input = ...
remove the input and make it
b = ...
or just change input to input1 or something else. This should resolve the problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KrabbyPatty |
