Home > Net >  How to make this def script from one file to another file in python
How to make this def script from one file to another file in python

Time:07-21

code in 1st file:

import random

def randomnumb():
    numbers = random.randint(1,999)
    if numbers < 10:
        numbers = f'00{numbers}'
    if numbers < 100 and numbers > 10:
        numbers = f'0{numbers}'

code in 2nd file:

from RandomGenerator import randomnumb

randomnumb()

I know that this script is not working, but I want to know know how to make somthing like:

from RandomGenerator import randomnumb
randomnumb(x)
print(x)

Output:

089

CodePudding user response:

randomnumb needs to return the value.

def randomnumb():
    numbers = random.randint(1,999)
    return f'{numbers:03d}'

x = randomnumb()
print(x)

CodePudding user response:

Your function didn't get the parameter and returns nothing.

But also your code isn't working because you assign type string to the numbers, and then trying to compare it below with an integer again.

Just keep all the string conversions at the end.

  • Related