1

I have written a function that takes multiple user entry and works with the data entered. The following function is in file1

def dimensions
    f = open("file.txt", "a")
        distance= (raw_input("Enter distance: "))
        height= raw_input("Enter height: "))
        length= raw_input("Enter length: "))
        f.write(fullInfo)
        f.close()

The following is in file2. File2 calls file1 with import This function dimensions is called as so:

    def calculation():
        print "1- Enter dimensions"
        print "2- Enter something else"
        choice = raw_input("")
        if choice == 1:
            file1.dimensions

The issue I am having with this is that if the user does not want to enter dimensions, and mistakenly entered 1 in the menu, he will still have to enter all three queries.

I tried importing file1 in file2 and code the following so that it may go back to the menu, but importing both files in one another doesn't seem to work. This was coded in file1

if distance == 'exit':
    file2.calculation

Any help or pointers as to how I can allow the user to break away from the questions and back to the menu would be appreciated. Thanks

Johny
  • 129
  • 11

2 Answers2

0

Add an additional option called option 0 which means that user wants to restart and add an if condition that checks if the user has entered 0

def dimensions
    f = open("file.txt", "a")
    distance= (raw_input("Enter distance: "))
    if distance == 0: # or any other value that may seem fit
            # handle the situation to restart the program
    height= raw_input("Enter height: "))
    length= raw_input("Enter length: "))
    f.write(fullInfo)
    f.close()
Banach Tarski
  • 1,821
  • 17
  • 36
  • My problem is with handling the situation if a value is entered. I don't know what to enter and calling the menu function doesn't work because the file it is is already calling the function dimension() – Johny Mar 28 '16 at 11:38
0
  • 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
jDo
  • 3,962
  • 1
  • 11
  • 30
  • Thank you for your answer. Most of these errors come from me trying to simplify the code to post here. My issue is that I would like the user to be able to go back to the calculation method (i.e. the menu) while inside the dimension method. The problem is that calculation calls dimension through 'import', thus I cannot import calculation in dimension, so there is no way of going back to the menu. – Johny Mar 28 '16 at 12:14
  • I'm re-writing the whole thing now hehe – jDo Mar 28 '16 at 12:51
  • @Johny You're welcome :) I initially had everything in one file but I split it up into "menu.py" and "get_dimensions.py" to fit your question. – jDo Mar 29 '16 at 06:28
  • @Johny Btw. if this answered your question, you should accept it. It'll give me some silly internet points and, more importantly, it'll keep the question from circulating as "unanswered". – jDo Mar 30 '16 at 22:29