Skip to content
Permalink
Browse files
Uploaded and added comments.
  • Loading branch information
gorausa committed Nov 29, 2018
1 parent 2a70706 commit 5fb5cd865d53736a747f47606d3af5ecc70c3740
Showing 1 changed file with 46 additions and 0 deletions.
@@ -0,0 +1,46 @@
import os

'''This function creating a new text file (if not exist) and storing two variables, name and city, which can be used further in different functions
Otherwise, check if data in created file exist.'''
def setup_time():
exists = os.path.isfile("settings.txt") #Returning boolean (True) if file "settings.thx" exist.
if not exists: #Otherwise, a new file will be created in write mode. Inputs name and city will be saved and stored.
f = open("settings.txt", "w+")
print("")
name = input("What shall I call you: ")
city = input("Which city are you in: ")
f.write("name:" + name + "\n")
f.write("city:" + city + "\n")
f.write("state:complete")
f.close()
else: #This else statement will be run if a text file already exists and check its data.
settings_file = open("settings.txt")
for setting in settings_file:
if "state:complete" in setting: #While name and city already exist (state:complete) in "settings.txt"
settings_file.close() #Just close the file
break

'''The purpose of this function is to delete text file (data inside) after the main function will end,
so that a new data (name and city) can be created and stored next time while main code run.'''
def reset():
exists = os.path.isfile("settings.txt") #To avoid the error, checking if the file exists
if exists:
os.remove("settings.txt") #If True, then deleting the file
setup_time() #Calling function setup_time to create a new file and new data (name and city)


'''The "arg" is taken from the main code. It is either 0 or 1, depend on which argument from the file we need, either name or city.'''
def load_setting(arg):
settings_file = open("settings.txt")
if arg == 0:
for setting in settings_file:
if "name:" in setting:
name = setting.replace('name:','') #Taking just the user input name. Will be used while the program is running and printing username/calling the user.
settings_file.close()
return name
elif arg == 1: #Similar to above, but argument city which was defined by the user as an input on beginning can be used in function weather_ask.
for setting in settings_file:
if "city:" in setting:
city = setting.replace('city:','')
settings_file.close()
return city

0 comments on commit 5fb5cd8

Please sign in to comment.