Lesson 15 small turtle formatted to reflect after-school summary

First, using the format distinguish between about today's format and formatting commands:
format:
'{0: A.Bf. 1} {2} {}'. Format = (1.11111, asdasda, 2)
where A, B are respectively the width ( and a string length), and a floating point reserved bits
and rear brackets must first, and separate each parameter corresponding to a position spaced
becomes as keyword parameters when 0.1.2 abc ,, in parentheses are required plus a =, b =, c =

Formatting commands
'% A.Bf'% · Data
AB with the format
F may be changed to other commands

Title:
4. It should be how to fill in if you want to display Pi = 3.14, format string in front of it?

'  '.format('Pi = ', 3.1415
{0}{1:.2f}

Programming problem:
to write a binary conversion program, program presentation follows (prompt, you can use binary decimal conversion bin () This BIF):
Here Insert Picture Description
Reprinted from: https: //www.jianshu.com/p/e9b8240a9109

t = 'Q'
while t == 'Q':
    temp = input('请输入一个十进制数:')
    number = int(temp)
    #这里必须要把temp变整型变量,因为#o或#x都是针对整型
    a = '% #x' % (number)
    b = '% #o' % (number)
    c = bin(number)
    print('十进制到十六进制:108到%s'%a)
    print('十进制到八进制:108到%s'%b)
    print('十进制到二进制:108到%s'%c)
    print('请选择输入Q为继续,否则按任意键退出',end='')
    t = input()

Better answer, use the built-in string functions

num = input("请输入一个整数(输入Q结束程序):")
while num.upper() != 'Q':    
    if num.isdigit():
        num = int(num)
        print('十进制 -> 十六进制:%d -> %#x'%(num,num))
        print('十进制 -> 八进制:%d -> %#o'%(num,num))
        print('十进制 -> 二进制:%d -> '%num,bin(num))
        num = input("请输入一个整数(输入Q结束程序):")
    else:
        if num == 'Q':
            break
        else:
            num = input("输入不合法,请输入一个整数(输入Q结束程序):")
            
```转载自:https://www.jianshu.com/p/e9b8240a9109
Published 17 original articles · won praise 1 · views 358

Guess you like

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105274949