This article mainly describes the python implementation of the specified requirements in reverse order to output a number of methods, involving Python for string traversal, judgment, output and other related operations skills, the need for friends can refer to the following
This example describes how Python implements a method of outputting a number in reverse order to the specified requirements. Share to everyone for your reference, as follows:
The problem is : Enter a number, follow the specified requirements to output the number in reverse, very simple, the following is the implementation:
#!usr/bin/env python#encoding:utf-8 "" __author__: Yishui Cold city Function: Reverse output A number if the number is a positive direct output such as: 177---> 771 if the number is negative, keep the minus sign, such as: -945--->-549 if the number is reversed with 0 results after 0 such as:--->1 if the number is very general cause overflow return 0 can be "def inverse_num (one_num): " " Reverse output a number" if one_num>99999999: return 0 elif one_num==0: return 0 else: If one_num>0: flag=true Else: flag=false one_num*=-1 one_num_list=list (str (one_num)) While one_num_list[-1]== ' 0 ': one_num_list.pop () tmp= ". Join (One_num_list[::-1]) if flag: return tmp else: return '-' +tmpif __name__ = = ' __main__ ': print "script home test Result:" one_num_list=[ 124,-345,1000,999999999,0] for one_num in one_num_list: print One_num, Inverse_num (one_num)
The results are as follows:
Python implements a method of entering multiple lines in idle