簡體   English   中英

檢查數字是否為素數,Python 拋出異常

[英]Check if number is prime with Python thowing an exception

I have a python function that is supposed to take integer parameter and process it according to the logic in the function and print out whether the number is prime or not. 我在 Visual Studio 2019 上運行我的 python 文件。它在參數上引發異常,我從用戶傳遞到 function .. 這是我從 Visual Studio 異常框unsupported operand type(s) for +: 'int' and 'str'

##Defining python function to check whether a certain number is prime or not
def check_Prime(o):
    isPrime=True
    for i in range(2,o):
        if(o%i==0):
          isPrime=False
    
    if(isPrime):
        print(o+" "+"is prime")
    else:
        print(o+" "+"is not prime")


prime=int(input("Enter the integer you would like to check for prime"))
check_Prime(prime)

使用f 字符串或格式化的字符串文字

print(f'{o} is prime')

您的錯誤指向連接intstr ,這是您無法做到的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM