我的程序关于寻找因子的问题 [英] Problem with my program about finding factors
问题描述
这是我旧问题的延续该程序在没有
This was a continuation to my older question The program was working fine without the
虽然为真:主要的()if input("Try Again? (Yes/No)").strip().upper() == 'No':打破
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
推荐答案
您在 main() 内部定义了 print_factors,这意味着它不是全局作用域可用的函数
You defined print_factors inside of main(), meaning it's not a function available to the global scope
给 main() 一个正文,并取消缩进 print_factors() 它应该可以解决您的问题
Give main() a body, and unindent print_factors() and it should resolve your issues
这篇关于我的程序关于寻找因子的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!