Importing

so l have two programs, the first is test.py


# List of Proper Divisions

#empty list to store proper divisors

divisors = []

def proper_divisor(n):

    for numbers in range (1,n):

        if n % numbers == 0:

            divisors.append(numbers)

    return divisors

print("The list of proper divisors are", divisors )            

def main():

    value = int(input("Enter the number: "))

    print("the proper divisors are",proper_divisor(value))

    #print("the dividend",value)

main()

The second is test2.py


from test import main

divisors = main()

print(divisors)

def perfect_numbers():

    summation = 0

    for numbers in divisors:

        summation += numbers

    if summation == value:

        print("this is a perfect number", value)

    return summation

print(perfect_numbers)

  1. Now when l import, the first problem is I am getting 2 inputs but l want just one input
    2.The second problem, is l want the value variable in the second program, but l don’t know how to bring it there

Thanks

You should take a little time to look up how imports work, it is not necessarily obvious and you seem to have missed something important-
https://docs.python.org/3/tutorial/modules.html

Long story short your import is running that first script and since it contains print statements and such you will get outputs from it.

What is the value variable? There is one that is part of main but that is within a function and follows normal scoping rules for a function so you wouldn’t directly access it in either script. If you wanted it, you’d have to return it from your function.

1 Like

Yes this is the one am talking about, but when it is returned, it is returned as an output, which, can’t be used, I finally want to see if the sum of the divisors is equal to the dividend

I have tried this


# List of Proper Divisions

#empty list to store proper divisors

divisors = []

def proper_divisor(n):

    

    for numbers in range (1,n):

        if n % numbers == 0:

            divisors.append(numbers)

    return divisors

print("The list of proper divisors are", divisors )            

def main():

    n = int(input("enter the dividend: "))

    print("the proper divisors are",proper_divisor(n))

#print("the dividend",value)

    

main()


from test import proper_divisor

n = int(input("Enter the dividend: "))
divisors = proper_divisor(n)








def perfect_numbers():
    summation = 0
    for numbers in divisors:
        summation += numbers
    if summation == n:
        print("this is a perfect number", 8)
    return summation

print(perfect_numbers)


am still getting two input statements from the program

You’ve lost me here, I’m sure you have a similar post that dove into the differences between simply printing from a function and the use of return. You use input in more than one place, if you run code containing that statement then it will run. If you can’t figure out what’s running when then either simplify your program into sections that do make sense or start using print to work out how your program flows (in the case perhaps warning your which input statement is about to be run would help).

1 Like

When you write code in a file intended to be a module from which you import functions, you may not want to include executable lines of code in the file. Functions don’t execute unless called. When you import from your test.py file, any lines of code that can execute do so, including:

1 Like

even if l remove that first input statement, it still appears twice

You’ll have to post your updated code for us to be of any assistance without having to guess at what you have done.

1 Like

Have you run the code and seen what l have done?

I have not run the code and I don’t plan to. I’m happy to provide guidance but I’m not going to debug every code you post, that’s something that you should work on yourself, it’s a very useful skill.

I’ve already provided a link to documentation which would be of use to you at this point so that you don’t work by trial and error. I have also suggested a route to help you debug. I suggest making use of those options.

I have tried debugging but can’t find a solution

So what have you done?

I have watched a tutorial on import function,

I read bit of that article but it started getting a bit complex, so l read the basics.

Then l was experimenting with the code abit

Thanks for sharing Its helpful for me to understand the best old town solo canoe data.

1 Like

Thanks for sharing. Plz, guide me during the process of how can import images. Because I lose some data of old town solo canoe data.

Someone Provide me the better solution for retrieve data of solo canoe.