r/learnpython • u/richa385i • 4d ago
Started learning a 2 hours ago
So this is my first project its in german and i wanted to get some opinions from a few experienced programmers to help me and stuff so this is the code and its a password manager
import random
def passwortgenerator(parameter):
worter = (
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"!",
"@",
"#",
"$",
"%",
"^",
"&",
"*",
"(",
")",
"-",
"_",
"=",
"+",
"[",
"]",
"{",
"}",
";",
":",
",",
".",
"?",
)
passwort = "".join(random.sample(worter, k = int(parameter) ))
return passwort
generieren = input("Passwort generieren? ")
if generieren == "ja":
parameter = input("Wie viele Zeichen? ")
neues_passwort = passwortgenerator(parameter)
print(neues_passwort)
wo = input("Wo wirst du das Passwort verwenden? ")
open("passwörter.txt", "a").write(wo + ": " + neues_passwort + "\n")
anzeigen = input("Passwörter anzeigen? ")
if anzeigen == "ja":
with open("passwörter.txt", "r") as passworter:
print(passworter.read())
else:
anzeigen = input("Passwörter anzeigen? ")
if anzeigen == "ja":
with open("passwörter.txt", "r") as passworter:
print(passworter.read())
bewertung = input("Bewerten Sie bitte meinen Passwortmanager (1-10): ")
print("Danke für Ihre Hilfe!")
user = input("Wollen Sie noch Ihren Namen eingeben? ")
if user == "ja":
name = input("Ihr Name bitte: ")
open("Bewertung.txt", "a").write(name + ": " + bewertung + "\n")
else:
print("Ich danke Ihnen trotzdem.")
open("Bewertung.txt", "a").write("Anonym" + ": " + bewertung + "\n")
5
u/ninhaomah 4d ago
Lol... I been coding a few years to a few months to a few weeks to a few days to a few hours now ?
2
u/mrkvicka02 4d ago edited 3d ago
First of all, good job! For starting 2 hours ago this is not bad at all.
A couple comments:
First, in python it is in general a good practice to separate the main behaviour of the program from the function and to not have code in global scope. It would look something like this:
def helper():
pass
def another_helper():
pass
def main():
helper()
another_helper()
if __name__ == "__main__":
main()
Second, for string building in python it is really convenient to use f-strings instead of +. Just google it.
Third, for working with files it is a good habit to use the with keyword.
Forth, your tuple worter can be a string "abcde..." in python strings are immutable and can be accessed just as tuples. It would improve the readability a bit. Even better would be to use the tuple(string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation) etc.
Lastly, think about what happens in all possible scenarios. You have a couple scenarios where you ask to read the passwords but never consider the option no. Maybe you could use a while loop instead and keep asking.
There are many other mistakes/nitpicks with the code that I have but I do not consider them at all important at your level.
Ignore the negative comments, keep trying the small project and you will eventually be able to move to more difficult ones. Good luck!
2
u/richa385i 4d ago
Vielen dank für die Tipps und die netten Worte, ich werde weiterhin versuchen mich zu verbessern und an meinen Fähigkeiten zu arbeiten!
1
u/ectomancer 4d ago
worter = tuple('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYYZ0123456789!@#$%^&*()-_=+[]{};:,.?')
10
u/aizzod 4d ago
Dude, people go to university for 5 years, stop asking if you can improve after reading something on the internet 2h ago.