hab ein kleines Programm und würde gerne wissen, ob ich Bugs übersehen habe, was es stilistisch vielleicht zu beachten gäbe, und vorallem, wo ich ErrorCheck mäßig noch was verbessern kann.
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Simple program to store some notes
FILENAME = "notes.txt" # must be in working directory
# make sure file exists
try:
f = open(FILENAME, "r")
f.close()
except FileNotFoundError:
f = open(FILENAME, "w")
f.close()
def print_notes():
with open(FILENAME, "r") as file:
print("Notes so far:")
while True:
line = file.readline()
if len(line) == 0:
break
print(line, end="")
def write_note(note):
with open(FILENAME, "a") as file:
if not note.isspace():
file.write(note + "\n")
def delete_notes():
with open(FILENAME, "w") as file:
print("Notes successfully deleted.")
def main():
print("=== Note Manager ===")
print("'show': print all" \
", 'del': delete all, 'done': quit.\n\n")
while True:
note = input("Enter note or command: ")
if note == "done":
break
elif note == "show":
print_notes()
elif note == "delete":
delete_notes()
else:
write_note(note)
if __name__ == "__main__":
main()
LG
HarteWare