简体   繁体   English

Python Prime计算器-不更新布尔值

[英]Python prime calculator - Not updating boolean value

from sys import argv

def prime():
  for i in range(2,num):
      if num % i == 0:
          print(f"{num} can be divisible by {i}.")
          checker = False

num = int(input("Enter the number you want to check is prime: "))
checker = True

if num < 2:
    print(f"{num} is a prime number.")
elif num == 0:
    print(f"{num} is not a prime number.")
elif num > 0:
    prime()
    print(f"Is this number a prime - {checker}.")
else:
    print(f"Please write number larger than 0.")

Hello guys, I've just started to code and this is probably a really simple question. 大家好,我刚刚开始编写代码,这可能是一个非常简单的问题。 I want my code to either find the factors of a number or print if a number is a prime - but my boolean value in the "prime" function never updates to False. 我希望我的代码查找数字的因数或在数字为质数的情况下打印-但我在“质数”函数中的布尔值永远不会更新为False。 Not sure why! 不知道为什么!

Thank you. 谢谢。

The reason why your prime() function is not 'updating' to False is because there is no return statement in the function. 您的prime()函数未“更新”为False的原因是因为该函数中没有return语句。

With it, the function looks like this: 有了它,函数看起来像这样:

def prime():
  checker = True
  for i in range(2,num):
      if num % i == 0:
          print(f"{num} can be divisible by {i}.")
          checker = False
  return checker

And the updated code looks like this: 更新后的代码如下所示:

from sys import argv

def prime():
  checker = True
  for i in range(2,num):
      if num % i == 0:
          print(f"{num} can be divisible by {i}.")
          checker = False
  return checker


num = int(input("Enter the number you want to check is prime: "))

if num < 2:
    print(f"{num} is a prime number.")
elif num == 0:
    print(f"{num} is not a prime number.")
elif num > 0:
    is_prime = prime()
    print(f"Is this number a prime - {is_prime}.")
else:
    print(f"Please write number larger than 0.")

However, this isn't the only way to solve your problem: it's not recommended and frowned upon by many programmers as bad practice - use the global statement (which looks like that's what you were trying to do originally). 但是,这不是解决问题的唯一方法: 很多程序员都不建议这样做,并且不赞成这样做 ,请使用global语句(这似乎是您最初试图做的事情)。

With it, the function looks like this: 有了它,函数看起来像这样:

def prime():
  global checker
  for i in range(2,num):
      if num % i == 0:
          print(f"{num} can be divisible by {i}.")
          checker = False

And the updated code looks like this: 更新后的代码如下所示:

from sys import argv

def prime():
  global checker
  for i in range(2,num):
      if num % i == 0:
          print(f"{num} can be divisible by {i}.")
          checker = False


num = int(input("Enter the number you want to check is prime: "))
checker = True

if num < 2:
    print(f"{num} is a prime number.")
elif num == 0:
    print(f"{num} is not a prime number.")
elif num > 0:
    prime()
    print(f"Is this number a prime - {checker}.")
else:
    print(f"Please write number larger than 0.")
def prime():
       global checker
       """ remaining logic as is"""

I think this would work. 我认为这会起作用。 Sorry for format as I'm writing on phone. 很抱歉,我在手机上写的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM