Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time # importing time module to record the currrent time.
- name=""
- score=0
- def welcome():
- global name, score
- name = input("Whats your name?")
- print ("Welcome",name)
- score+=len(name)# silly increment of score based on name length
- saving()
- def saving():
- global name, score
- try: # try to open testfile.txt
- file = open("testfile.txt","a") # if file is found open in append mode 'a'
- except FileNotFoundError: #if testfile.txt is not found
- file = open("testfile.txt","w") # if not found create a new file and open in write mode 'w'
- file.write(name) # write the name variable to file
- file.write("\n") # make a new line
- file.write(str(time.ctime())) # write the current time 'ctime()'
- file.write("\n")
- file.write(str(score)) # write the name variable to file as a string (str)
- file.write("\n")
- time.sleep(5) # wait for 5 seconds (to demonstrate time change)
- file.write(str(time.ctime())) # write the updated time
- file.write("\n")
- file.write("--------------------")
- file.write("\n")
- file.close() # close the file
- welcome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement