Python practice questions 3.20 three digits in reverse order

The program reads in a positive 3 digits at a time, and then outputs the digits in reverse order. Note: When the input number contains a trailing 0, the output should not have a leading 0. For example, input 700, the output should be 7.

Input format:

Each test is a 3-digit positive integer.

Output format:

Output the number in reverse order.

code show as below:

#!/usr/bin/python
# -*- coding: utf-8 -*-

n = str(input())
s = list(n)
m = s[::-1]

if m[0] != '0':
    l1 = list((m[0],m[1],m[2]))
    print("".join(l1))
elif m[0] == '0':
    if m[1] != '0':
        l2 = list((m[1],m[2]))
        print("".join(l2))
    elif m[1] == '0':
        l3 = list((m[2]))
        print("".join(l3))

The list is learned in vain, alas, the stupid way to judge if statement.

It's 22:53 Beijing time, write another one and watch video learning.


Just opened Baidu and got it in one sentence.
code show as below:

n = input()
rever_n = ''.join(list(reversed(n)))
print(int(rever_n))

The reversed () function means reverse, and returns a reverse iterator (tuple, string, list, or range) can be used.

Turning it over also erases 0 automatically.

The final output of the program is still of type int. Learn the built-in methods of Python. Come on, Ollie!


There is always a book and fitness on the road

Guess you like

Origin www.cnblogs.com/Renqy/p/12723140.html