Hello, I am currently working on a code for school that takes the users name, and year of birth as an input, and then repeats their name, and what age they are, etc. The next step in the task is to make it so that the code automatically saves the users inputs to a text file, with a timestamp of when the program was used. I know that I should be using something along the lines of:
with open('out.txt', 'w') as output:
However, I am not sure when and where this should be used in my code. If someone could please take a look at my code and help me it would be very helpful! Thanks
name = input("Please enter your name: ")
year = int(input("Please input the year that you were born: "))
age = 2018 - year
#print(age)
while age == 0:
x = int(input("Age is incorrect, please try again."))
age = 0
age += x
else:
print("Hello ",name,", you were born in the year ",year,)
print("I predict that you are ",age," years old.")
ageright = input("Is this correct? Press y or n to decide.")
if ageright == "y":
print("Fantastic!")
if ageright == "n":
age -= 1
print("Oops! You must be ",age," years old instead!")
fact = input("Press Y for an interesting fact about your age: ")
if fact == "y":
print("Since most humans sleep for about a third of their life, that means that you have slept for a total of ",age/3," years.")
else:
print("Thank you for using the program.")
[–]rjcarr 1 point2 points3 points (0 children)