fullInfo
is not defined in the code you posted.
if choice == 1:
will never be true since raw_input("")
/ choice
is a string and needs to be cast to int
for a comparison with 1 to make sense.
In the snippet below, dimensions
is lacking parentheses and colon, and distance
should not be nested/indented under f = open("file.txt", "a")
def dimensions
f = open("file.txt", "a")
distance= (raw_input("Enter distance: "))
You don't actually call a function unless you add parentheses. This applies to calculation
below:
if distance == 'exit':
file2.calculation
"(...) importing both files in one another (...)" is called cyclic import or circular import. There are ways to make it work but it's better to avoid it if possible. More on that here if you're interested.
Yeah, sorry... I was bored
menu.py
import json
import os
import get_dimensions
def dims_load(dims_json_file):
""" you hand this function a filename and it returns a dictionary of dimensions """
with open(dims_json_file, "r") as dims_json:
return json.loads(dims_json.read())
def dims_save(dims_dict, dims_json_file):
""" you hand this function a dictionary and a filename to save the dimensions """
with open(dims_json_file, "w") as dims_json:
dims_json.write(json.dumps(dims_dict))
def menu():
dimensions_file = "dimensions.json"
file_exists = os.path.isfile(dimensions_file)
print "1- Enter dimensions"
print "2- Load previous dimensions"
print "3- Johny's funky function!"
choice = raw_input()
try:
choice = int(choice)
except ValueError as e:
print "Only numbers can be entered. Error details:", str(e)
if choice == 1:
dims_dict = get_dimensions.prompt_user()
if "exit" in dims_dict.values():
print "User chose to exit get_dimensions()"
menu()
else:
if file_exists:
print "overwriting your precious data!"
dims_save(dims_dict, dimensions_file)
elif choice == 2:
if file_exists:
print dims_load(dimensions_file)
else:
print "No dimensions file found. Can't print"
elif choice == 3:
print "Johny's funky function has yet to see the light of day..."
else:
print "I have no idea what you want. Make up your mind, human!?"
menu()
get_dimensions.py
def prompt_user():
dims = {
"distance":"",
"length":"",
"height":""
}
dims_keys = dims.keys()
current = 0
while current < len(dims_keys):
dims[dims_keys[current]] = raw_input("Enter " + dims_keys[current] + " (type 'exit' to return to menu): ")
current += 1
if "exit" in dims.values():
return dims
return dims