关于python导入模块后发现重复执行

直接贴代码

greet_users.py

unprinted_designs = ['iphone case', 'robot pendant','dodecahe']
complete_models = []
i = 0

def print_design(unprinted, completed, x):
    while unprinted:
        current_design = unprinted.pop()
        print("Printing model " + current_design + " " + str(x))
        completed.append(current_design)
        x = x + 1

print_design(unprinted_designs[:], complete_models, i)

pizza_even_even.py

#from greet_users import print_design
import greet_users

unprinted_designs = ['iphone case', 'robot pendant','dodecahe']
complete_models = []
i = 0

greet_users.print_design(unprinted_designs, complete_models, i)

以下是执行pizza_even_even.py的结果

C:\Python38\python.exe C:/Users/snowm/Desktop/文件夹/PycharmProject/pizza_even_even.py
Printing model dodecahe 0
Printing model robot pendant 1
Printing model iphone case 2
Printing model dodecahe 0
Printing model robot pendant 1
Printing model iphone case 2

Process finished with exit code 0

greet_users.py改成下面这样就可以

只要看最后一行,最后一行代码被我注释掉了

unprinted_designs = ['iphone case', 'robot pendant','dodecahe']
complete_models = []
i = 0

def print_design(unprinted, completed, x):
    while unprinted:
        current_design = unprinted.pop()
        print("Printing model " + current_design + " " + str(x))
        completed.append(current_design)
        x = x + 1

def report(completed):
    for completed_model in completed:
        print(completed_model)

#print_design(unprinted_designs[:], complete_models, i)

网上找了很多,对于初学者来说都有点难看懂,问了学长才知道咋解决
因为python在导入模组的时候把greet_users.py中的那个语句也导入了,总之就注释掉就ok了

你可能感兴趣的:(日记,Python)