Problem with my program about finding factors

和自甴很熟 提交于 2019-12-04 05:42:52

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/56558810/problem-with-my-program-about-finding-factors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!