Advertisement
mrScarlett

save to file text adventure

Nov 10th, 2017
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. import time # importing time module to record the currrent time.
  2. name=""
  3. score=0
  4. def welcome():
  5.     global name, score
  6.     name = input("Whats your name?")
  7.     print ("Welcome",name)
  8.     score+=len(name)# silly increment of score based on name length
  9.     saving()
  10. def saving():
  11.     global name, score
  12.     try: # try to open testfile.txt
  13.         file = open("testfile.txt","a") # if file is found open in append mode 'a'
  14.     except FileNotFoundError: #if testfile.txt is not found
  15.         file = open("testfile.txt","w") # if not found create a new file and open in write mode 'w'
  16.        
  17.     file.write(name) # write the name variable to file
  18.     file.write("\n") # make a new line
  19.     file.write(str(time.ctime())) # write the current time 'ctime()'
  20.     file.write("\n")
  21.     file.write(str(score)) # write the name variable to file as a string (str)
  22.     file.write("\n")
  23.     time.sleep(5) # wait for 5 seconds (to demonstrate time change)
  24.     file.write(str(time.ctime())) # write the updated time
  25.     file.write("\n")
  26.     file.write("--------------------")
  27.     file.write("\n")
  28.     file.close() # close the file
  29.  
  30. welcome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement