Problem with my program about finding factors

2020-05-07 10:00发布

This was a continuation to my older question The program was working fine without the

while True: main() if input("Try Again? (Yes/No)").strip().upper() == 'No': break

but when i added it, the problem rose

I was trying to make the program start by asking the user a number and it shows a factor then i loop it and ask the user if he wants another number and it repeats if the user wants to repeat it

def main():

 def print_factors(x):
    print("The factors of",x,"are:")
    for i in range(1, x + 1):
    if x % i == 0:
        print(i)


try:
    num = int(input("Enter a number: "))
    print_factors(num)
except ValueError:
    print("Sorry, I didn't understand that.");

while True:
    main()
    if input("Try Again? (Yes/No)").strip().upper() == 'No':
    break


line 12, in <module>
    print_factors(num)
NameError: name 'print_factors' is not defined

when the program is ran this was the end result

标签: python-3.6
1条回答
家丑人穷心不美
2楼-- · 2020-05-07 10:32

You defined print_factors inside of main(), meaning it's not a function available to the global scope

Give main() a body, and unindent print_factors() and it should resolve your issues

查看更多
登录 后发表回答