Nov-19-2019, 10:24 AM
The original code is here.
The goal of this code is to have it change the two-letter code every day and write it to the file.
This is the problem code. Specifically lines 43 - 45.
If I leave the "not" out of line 43 it writes a different code on every run as expected. But if I add
the "not" to line 43 it will change the code on the first run but not on any runs after that no matter how I change
Why does it ignore my changes?
The goal of this code is to have it change the two-letter code every day and write it to the file.
This is the problem code. Specifically lines 43 - 45.
import datetime import os.path import random import string DATE = datetime.datetime.now() DAY = DATE.day def transfer_file(): """Check if file exists.""" if os.path.exists("transfer.txt"): return else: with open("transfer.txt", "w") as file: return def code_file(): """Check if file exists.""" if os.path.exists("code.txt"): return else: with open("code.txt", "w") as file: return def set_expiration(): """Sets date format and adds a day.""" expires = f"{DATE.month}/{DAY + 1}/{DATE.year}" return expires def set_code(): """Sets random two letter code.""" letters = string.ascii_uppercase code_1 = random.choice(letters) code_2 = random.choice(letters) day_code = f"{code_1}{code_2}" with open("code.txt", "r") as file: code = "".join(file.readlines()) print(code) if str(DAY) not in set_expiration(): with open("code.txt", "w") as file: file.write(day_code) else: return "".join(code) transfer_file() code_file() set_code()First I don't understand why it won't write to the file on the first run unless I change line 43 to
if str(DAY) in set_expiration():
.If I leave the "not" out of line 43 it writes a different code on every run as expected. But if I add
the "not" to line 43 it will change the code on the first run but not on any runs after that no matter how I change
str(DAY)
. I usually change it by taking the + 1
out of line 29. expires = f"{DATE.month}/{DAY + 1}/{DATE.year}"
Why does it ignore my changes?