dimanche 24 mai 2020

How do I import functions and then test them in a separate script in Python

Thanks in advance for you help. I really appreciate it. For this lab I am supposed to write a script that will test each of functions in the lab_functions.py script I had written. I have attached the assignment so you can better understand what I mean. The main issue I seem to be having is that the variables from the imported code don't carry over. What should I do about this? The code I am using for this lab so far is:

import lab_functions
def test_square_each():
    lst=[1,2,3,4,5]
    square_lst=[1,4,9,16,25]
    if square_lst == square_each(numberfile):
        print("Square each: Passed")
    else:
        print("Square each: Failed")
    print("Original: ",last)
    print("Modified: ",square_lst)

def test_sum_list():
    lst=[1,2,3,4,5]
    sum_lst=15
    if sum_lst == sum_list(numberfile):
        print("sum list: Passed")
    else:
        print("sum list: Failed")
    print("List: ",last)
    print("Sum: ",sum_lst)

def main():
    lab_functions.main()
    test_square_each()
    test_sum_list()

main()

The code That I am importing from the previous lab is:

def sum_list(str_list):
    sum_of_numbers = sum(str_list)
    return sum_of_numbers

def square_each(str_list):
    for line in range(len(str_list)):
        str_list[line] = eval(str_list[line])
        str_list[line]= str_list[line]*str_list[line]
    return str_list

def main():
    file=input("Enter a file name that you would like to use: ")
    numberfile=open(file).readline().split(" ")
    print("The numbers that were input are: ")
    for line in numberfile:
        print(line)
    print("Each number squared individually is: \n",square_each(numberfile))
    print("The value of all squared numbers summed together is: ",sum_list(numberfile))
main()

This is the assignment that I am trying to figure out: enter image description here

Aucun commentaire:

Enregistrer un commentaire