日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python程序反转给定数字(2种不同方式)

發(fā)布時(shí)間:2023/12/1 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python程序反转给定数字(2种不同方式) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Take input number from the user and print its reverse.

從用戶處獲取輸入號(hào)碼并打印其反面。

Example:

例:

Input:12345Output:54321

Here, we are implementing program to reversing a given number using 2 different ways.

在這里,我們正在實(shí)施程序,以使用2種不同的方式來(lái)反轉(zhuǎn)給定的數(shù)字 。

1) Famous approach for reversing the number: Take input from the user and typecast into an integer, then iterate in the loop till num is not become zero, inside the loop:

1) 逆轉(zhuǎn)數(shù)字的著名方法 :從用戶輸入并將其類型轉(zhuǎn)換為整數(shù),然后在循環(huán)中循環(huán)直到num在循環(huán)內(nèi)不為零:

  • Find out the remainder.

    找出其余的。

  • Using this: rev_num = rev_num * 10 + remainder.

    使用這個(gè):rev_num = rev_num * 10 +余數(shù)。

  • Update that number by diving by 10.

    通過(guò)跳水10來(lái)更新該數(shù)字。

  • After coming out of the loop printing the reverse number.

    退出循環(huán)后,打印反向編號(hào)。

if __name__ == "__main__" :# take string input from usernum = int(input('Enter a number: '))rev_num = 0# iterate the loop till num is not equal to zerowhile(num) :rem = num % 10rev_num = rev_num* 10 + remnum //= 10print('Reverse number is: ', rev_num)

Output

輸出量

Enter a number: 12345 Reverse number is: 54321

2) Make a user-defined function for reversing the Number: Take input from the user and typecast into integer, thenreverseNum() function call.

2) 制作一個(gè)用于反轉(zhuǎn)Number的用戶定義函數(shù) :從用戶那里輸入輸入并將其類型轉(zhuǎn)換為整數(shù),然后調(diào)用verseNum()函數(shù)。

Inside the function:

函數(shù)內(nèi)部:

  • Iterate in the loop till num does not become zero:

    在循環(huán)中迭代,直到num不為零:

  • Find out the remainder.

    找出其余的。

  • Using this: rev_num = rev_num * 10 + remainder.

    使用這個(gè):rev_num = rev_num * 10 +余數(shù)。

  • Update that number by diving by 10.

    通過(guò)跳水10來(lái)更新該數(shù)字。

  • After coming out of the loop returning the reverse number to the main.

    退出循環(huán)后,將反向編號(hào)返回到主編號(hào)。

# define a function for finding # reverse of the number def reverseNum(num) :rev_num = 0# iterate the loop till num is not equal to zerowhile(num) :rem = num % 10rev_num = rev_num* 10 + remnum //= 10return rev_num# Main() method if __name__ == "__main__" :# take string input from usernum = int(input('Enter a number: '))print('Reverse number is: ', reverseNum(num))

Output

輸出量

Enter a number: 12345 Reverse number is: 54321

翻譯自: https://www.includehelp.com/python/program-to-reverse-a-given-number-2-different-ways.aspx

總結(jié)

以上是生活随笔為你收集整理的Python程序反转给定数字(2种不同方式)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。