【问题标题】:Python 3. How do you assign a variable in a function to use in the outer scope of the function?Python 3. 如何在函数中分配变量以在函数的外部范围内使用?
【发布时间】:2018-07-16 01:26:57
【问题描述】:

我对编程非常陌生。我一直在做一个项目,要求用户导入一个数字,该数字经过一个数学系列。然后将输出放入函数中以查找数字的因数。从那里我试图找到素数的因素? 这就是我到目前为止所拥有的。

enter code here####################################
n = int(input("Enter the n value"))
num = sum(10**x for x in range(n))
print("S",n,"is", num)
#####################################
# Factors 
#function name nfactors
def nfactors(x):
   # This function takes a number and prints the factors
  print("The factors of",x,"are:")
  for i in range(1, x + 1):
      if x % i == 0:
          print(i)

fact = nfactors(num)
print(fact)
#####################################
print('The prime numbers are:')

if fact > 1:
   # check for factors
   for i in range(2,fact):
       if (fact % i) == 0:

           break
   else:
       print(fact)

我知道这是一个糟糕的编程,但我正在尝试通过做这个项目来学习。然后我怎样才能将收到的因子作为函数的输出并找出哪些因子是素数。我无法弄清楚如何在函数内部命名变量并在函数外部使用它,我不知道这是否可能。如果您需要任何澄清,请告诉我。谢谢你的帮助。

【问题讨论】:

    标签: python python-3.x function variables


    【解决方案1】:

    我非常希望您尝试找到 n 而不是 num 的因数,因为找到 num 的因数 n=12 是一个很大的问题,而且需要几分钟甚至用我优化的代码甚至几个小时。

    无论如何,假设您想为n 而不是num 找到它,下面的代码应该可以完成您的工作。

    如果您真的想找到num 的因子,此代码也可以正常工作,但会花费太多时间。只需将factors = factors(n) 更改为factors = factors(num)。

    from math import sqrt
    #Block 1 (Correct)
    n = int(input("Enter the n value"))
    num = sum(10**x for x in range(n))
    print("S",n,"is", num)
    
    #---------------------------------------------------
    
    def factors_(x):
        # This function takes a number and prints the factors
        print("The factors of",x,"are:")
        list_of_factors = []
        for i in range(1, x + 1):
            if x % i == 0:
                list_of_factors.append(i)
        return(list_of_factors) #This returns a list of factors of a given number x
    
    factors = factors_(n) #I am assuming you want to find the prime factors of n instead of num
    print(factors)
    
    #------------------------------------------------------------------------------------
    
    def is_prime(num): #This function returns True(1) if the number is prime else Flase(0)
        if num == 2:return 1
        if num%2 == 0:return 0
        i = 3
        while i < int(round(sqrt(num)) + 1):
            if num % i == 0:return 0
            i += 2
        return 1
    #---------------------------------------------------------------------------------------    
    prime_factors = []
    for i in factors:
        if is_prime(i):
            prime_factors.append(i)
    
    print("The prime factors of the numbers are:")
    print(prime_factors)
    

    【讨论】:

      【解决方案2】:
      def nfactors(x):
      # This function takes a number and prints the factors
      print("The factors of",x,"are:")
      for i in range(1, x + 1):
          if x % i == 0:
              print(i)
              return i 
      
      fact = nfactors(num)
      

      使用 return 关键字返回 i 的值或您想在函数外使用的任何值!

      【讨论】:

      • 我有点困惑。在我的情况下,我会在函数之前命名 var (x) 。然后在我通过for循环发送它之前,我会使用全局x。然后在函数之外我会打印变量(x)?
      • 其实我误解了你的问题,我会为你更新答案。
      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 2012-08-18
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多