Python implementation outputs a number in reverse order of specified requirements

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.