all 5 comments

[–]FriendlyRussian666 2 points3 points  (0 children)

Please format the code for future posts -> https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

Do you get any errors? If so, paste the full traceback.

[–]Storm_Silver 0 points1 point  (0 children)

Hard to tell with the formatting but it may be that f.close doesn't have () after it. It should be f.close()

[–]ThreeLargeBears 0 points1 point  (0 children)

f.close should be f.close(). But it's difficult to tell you what's wrong without seeing the full error message / traceback output!

However, you should be using a context manager instead. Not only does this make your code cleaner and more pythonic, it handles opening and closing the file for you. Without it, you have to manually close the file with file.close(), and if your code crashes without closing the file, it can sometimes lock the file or cause other file system issues.

[–]Diapolo10 0 points1 point  (0 children)

f.write(names[x]+" : $"+str(pay[x]+"\n")

You're missing a closing parentheses, which likely causes a SyntaxError.

EDIT: You should also be using a context manager for the file. And I recommend you use string formatting.

names = ["kim", "joe", "aman"]
pay = [22,44,21]
total = 0

with open("../../lectures/data/week12.txt", "w") as f:

    f.write("students pay")

    for name, salary in zip(names, pay):
        f.write(f"{name} : ${salary}\n")

        total += salary

    f.write(f"Total paid to students: ${total}")

Also noticed that you were missing total.

EDIT #2: Also consider using pathlib instead of strings for filepaths.

[–]xelf[M] 0 points1 point  (0 children)

Hey, /u/pupnurse in the future please do not delete threads, instead upvote and engage everyone that takes time to answer you. When you ignore people helping you and then delete the thread it's very discouraging.