寫一個 Python 程序,用一個實際例子找到一個數字的最後一位。
尋找數字最後一位的 Python 程序
這個 python 程序允許用戶輸入任何整數值。接下來,Python 將查找用戶輸入值的最後一位。
# Python Program to find the Last Digit in a Number
number = int(input("Please Enter any Number: "))
last_digit = number % 10
print("The Last Digit in a Given Number %d = %d" %(number, last_digit))
使用函數查找數字最後一位的 Python 程序
一個數字程序中的這個 Python 最後一位同上。但是這次,我們分離了 Python 邏輯,並將其放在單獨的函數中。
# Python Program to find the Last Digit in a Number
def lastDigit(num):
return num % 10
number = int(input("Please Enter any Number: "))
last_digit = lastDigit(number)
print("The Last Digit in a Given Number %d = %d" %(number, last_digit))
Python 最後一位輸出
Please Enter any Number: 1925
The Last Digit in a Given Number 1925 = 5
>>>
Please Enter any Number: 90876
The Last Digit in a Given Number 90876 = 6
原創文章,作者:SI54N,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/128970.html