Python 程序:檢查數字是不是間諜數字

編寫一個 Python 程序來檢查一個數字是否是間諜數字,或者是否使用 for 循環。例如,如果數字的總和等於一個數字中各個數字的乘積,那麼它就是一個間諜號。在這個 Python 間諜號的例子中,我們使用了一個 while 循環來將數字分成數字,並找到這些數字的總和和乘積。if 語句檢查總和是否等於乘積,如果為真,則檢查間諜號。

Number = int(input("Enter the Number to Check Spy Number = "))
Temp = Number
Sum = 0
prod = 1

while Temp > 0:
    lastDigit = Temp % 10
    Sum = Sum + lastDigit
    prod = prod * lastDigit
    Temp = Temp // 10

print("The Sum of the Digits     = %d" %Sum)
print("The Product of the Digits = %d" %prod)

if Sum == prod:
    print("\n%d is Spy Number." %Number)
else:
    print("%d is Not a Spy Number." %Number)

Python 程序使用遞歸或遞歸函數來檢查一個數字是否是間諜數字。

total = 0
prod = 1
reminder = 0
def spyNumber(number):
    global reminder, total, prod
    if(number > 0):
        reminder = number % 10
        total = total + reminder
        prod = prod * reminder
        spyNumber(number // 10)
        return total, prod
    else:
        return 0

Number = int(input("Enter the Number to Check Spy Number = "))
total, prod = spyNumber(Number)

print("The Sum of the Digits     = %d" %total)
print("The Product of the Digits = %d" %prod)

if total == prod:
    print("\n%d is Spy Number." %Number)
else:
    print("%d is Not a Spy Number." %Number)
Enter the Number to Check Spy Number = 1421
The Sum of the Digits     = 8
The Product of the Digits = 8

1421 is Spy Number.

這個 Python 程序列印從 1 到 n 的間諜號或者某個範圍內的間諜號。

MinSpy = int(input("Please Enter the Minimum Spy Value: "))
MaxSpy = int(input("Please Enter the Maximum Spy Value: "))

for i in range(MinSpy, MaxSpy + 1):
    Temp = i
    Sum = 0
    prod = 1
    while Temp > 0:
        lastDigit = Temp % 10
        Sum = Sum + lastDigit
        prod = prod * lastDigit
        Temp = Temp // 10

    if Sum == prod:
        print(i, end = '   ')
Please Enter the Minimum Spy Value: 1
Please Enter the Maximum Spy Value: 10000
1   2   3   4   5   6   7   8   9   22   123   132   213   231   312   321   1124   1142   1214   1241   1412   1421   2114   2141   2411   4112   4121   4211 

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/270847.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-16 13:38
下一篇 2024-12-16 13:39

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python實現畫筆方向改變

    本文將介紹如何在Python中實現畫筆方向改變,讓畫筆以中心為軸旋轉。 一、Tkinter庫概述 Tkinter是Python自帶的GUI庫,可用於創建各種GUI應用程序。在Pyt…

    編程 2025-04-29
  • 運維Python和GO應用實踐指南

    本文將從多個角度詳細闡述運維Python和GO的實際應用,包括監控、管理、自動化、部署、持續集成等方面。 一、監控 運維中的監控是保證系統穩定性的重要手段。Python和GO都有強…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python中new和init的區別

    new和init都是Python中常用的魔法方法,它們分別負責對象的創建和初始化,本文將從多個角度詳細闡述它們的區別。 一、創建對象 new方法是用來創建一個對象的,它是一個類級別…

    編程 2025-04-29

發表回復

登錄後才能評論