Пример #1
0
def main():
    print()
    # Try defining arguments
    # If there is no argument, prompt the user that s/he can input one, and continue
    actions = sys.argv[1:]
    # Initiate necessary values
    no_Help = True
    valid_Argument = True
    # If there are arguments, proceed with arguments
    if actions == []:
        print('You can specify arguments on this little program!')
        print('Use \'python', sys.argv[0], '-h\' to see more!\n')
    else:
        if ('-h' in actions) or ('--help' in actions):
            no_Help = False
            print_Output(
                'This program (supposedly) solves a 3-by-3 magic square\n\n-h OR --help\tShow this help section\n-v OR --verbose\tShow more output to see how the program runs\n-o OR --output\tPrint all console outputs to a file named \'Magic_Square_v2.log.txt\'\n\t\t(Note that all of the data in that file, if exist, will be cleared)\n'
            )
        else:
            if ('-v' in actions) or ('--verbose' in actions):
                value_Global.verbose = True
                actions = [x for x in actions if x not in ['-v', '--verbose']]
            if ('-o' in actions) or ('--output' in actions):
                value_Global.output = True
                value_Global.output_File = open('Magic_Square_v2.log.txt', 'w')
                actions = [x for x in actions if x not in ['-o', '--output']]
            if actions != []:
                print('Arguments invalid!')
                valid_Argument = False
                print('Use \'python', sys.argv[0], '-h\' to see more!\n')
    verbose_Print('Calculation is initiating...\n\n')
    if no_Help and valid_Argument:
        calculation()
Пример #2
0
def get_number(numbers):
    # Initiate iteration variable
    i = 1
    # Run the following 9 times
    while i < 10:
        # Prompt for and collect input
        x = 'Input your integer ' + str(i) + ' (Leave blank for ' + str(
            i) + '): '
        print(x, end='')
        user_Input = input('')
        if value_Global.output:
            value_Global.output_File.write(x + user_Input + '\n')
        # See if user has input
        # If not, use the defalut and ask for the next input
        if user_Input == '':
            i += 1
            verbose_Print('User has not chosen input. Using default value\n')
        # If the user does have an input, check if the input is legit
        else:
            # Check if the input is legit by trying to convert it into an integer
            try:
                input_Integer = int(user_Input)
            # If ValueError exception is caught, prompt the user that the value is not legit
            except ValueError:
                print_Output('This is not a legit value!')
                verbose_Print('ERROR: Cannot parse integer!')
                print_Output('')
            # Only proceed to next number if this one is legit
            else:
                numbers[i - 1] = input_Integer
                i += 1
                verbose_Print(
                    'Integer parsing successful, onto next integer\n')
    return numbers
Пример #3
0
def calculation():
    print_Output('')
    # Initiate necessary values
    numbers = list(range(1, 10))
    # Ask for input using get_number(numbers) function
    numbers = for_Input.get_number(numbers)
    # Start calculation
    verbose_Print('\nCalculation starting, recording start time\n\n')
    start = time.time()
    # Sort the numbers
    verbose_Print('Sorting numbers')
    numbers.sort()
    # Calculate the average, integerize it, and check for the legitimacy of average using check_Average(numbers_Average)
    verbose_Print('Calculating the target sum of each row and column')
    numbers_Average = sum(numbers) / 3
    value_Calc.target = int(numbers_Average)
    # Set the central number at the center and remove it from the number list
    verbose_Print('Determining the central number')
    value_Calc.center = numbers[4]
    del numbers[4]
    # Only continue if the average and the center are legit
    verbose_Print(
        'Checking the legitimacy of the target sum and central number\n\n')
    if for_Vali.check_Average(numbers_Average) and for_Vali.check_Center():
        success = for_Calc.calc(numbers)
    else:
        verbose_Print('\nCheck failed, terminating calculation\n')
    verbose_Print('\nCalculation ending, recording end time\n')
    end = time.time()
    # If calculation successful, print the answer using show_Square(success)
    if success != False:
        for_Result.show_Square(success)
    for_Result.show_Time(start, end)
    input('\n\nPress enter key to exit...')
Пример #4
0
def check_Average(numbers_Average):
    # Check if the average is integer
    # If not so, prompt the user and set flag not to continue
    # Else, continue
    if not float(numbers_Average).is_integer():
        print_Output('These numbers cannot make a 3-by-3 magic square!')
        verbose_Print('ERROR: Average not an integer!')
        return False
    else:
        return True
Пример #5
0
def check_Center():
    # Check if the center is one-third of the target sum
    # If not so, prompt the user and set flag not to continue
    # Else, continue
    if not 3 * value_Calc.center == value_Calc.target:
        print_Output('These numbers cannot make a 3-by-3 magic square!')
        verbose_Print('ERROR: No proper center!')
        return False
    else:
        return True
Пример #6
0
def show_Square(success):
    print_Output('\nYour magic square is:')
    print_Output(
        '(There is only one but you can (probably) rotate and/or flip it!)\n')
    for x in success:
        print_Output(str(x))
    print_Output('\nThe sum of each line is ' + str(value_Calc.target) + '\n')
Пример #7
0
def verbose_Print(x):
    if value_Global.verbose:
        print_Output(x)
Пример #8
0
def show_Time(start, end):
    verbose_Print('\nCalculating calculation time')
    t = end - start
    print_Output('Calculation time: ' + str(format(1000 * t, '.3f')) +
                 ' milliseconds')