Skip to content

Commit 8cc52d1

Browse files
committed
添加格式化打印错误集合板块
1 parent 9facfd6 commit 8cc52d1

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

chapter_1/print_2.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
strHello = "the length of (%s) is %d" %('Hello Watermelon',len('Hello Watermelon'))
1414
print(strHello) # the length of (Hello Watermelon) is 16
1515

16+
strHello = "Hello Watermelon"
17+
print(strHello) # Hello Watermelon
18+
print ("%.*s" % (4,strHello)) # Hell
19+
print ("%.3s " % (strHello)) # Hel
20+
# print ("%.*S" % (4,strHello)) # ValueError: unsupported format character 'S' (0x53)
21+
# print ("%.3S " % (strHello)) # ValueError: unsupported format character 'S' (0x53)
22+
print ("%s" % ("Hello Watermelon")) # Hello Watermelon
1623
print('\n')
1724
print('\n')
1825

@@ -444,4 +451,104 @@
444451
print('\n')
445452
#------------------------------------------------------------------------------------------------格式化打印数字Number end
446453

454+
#------------------------------------------------------------------------------------------------格式化打印错误集合 start
455+
print('格式化打印错误集合 start ------------------------------------------------------------------------------------------------')
456+
457+
print("逗号错误 --------------------------------")
458+
# 请注意`左参数`和`右参数`之间不存在逗号,如果写了逗号,会报错。
459+
460+
# print("%s,%s", % ("hello","watermelon"))
461+
# SyntaxError: invalid syntax
462+
463+
print("进制转换错误 --------------------------------")
464+
465+
print("\t\t\t\t数据类型必须是小写字母")
466+
# `八进制类型`,`十进制类型`和`字符串类型`的缩写必须是`小写字母`,换成`大写字母`会报错。
467+
468+
# print ("%O " % ("Hello Watermelon"))
469+
'''
470+
Traceback (most recent call last):
471+
File "<pyshell#15>", line 1, in <module>
472+
print ("%O " % ("Hello Watermelon"))
473+
ValueError: unsupported format character 'O' (0x4f) at index 1
474+
'''
475+
476+
# print ("%D " % ("Hello Watermelon"))
477+
'''
478+
Traceback (most recent call last):
479+
File "<pyshell#13>", line 1, in <module>
480+
print ("%D " % ("Hello Watermelon"))
481+
ValueError: unsupported format character 'D' (0x44) at index 1
482+
'''
483+
484+
# print ("%S " % ("Hello Watermelon"))
485+
'''
486+
Traceback (most recent call last):
487+
File "<pyshell#14>", line 1, in <module>
488+
print ("%S " % ("Hello Watermelon"))
489+
ValueError: unsupported format character 'S' (0x53) at index 1
490+
'''
491+
492+
print("\t\t\t\t数据类型必须是数字类型")
493+
# 做`类型转换`的时候,`浮点类型`和`十进制类型`需要传入的参数必要是`数字类型`的。
494+
# print ("%d " % ("Hello Watermelon"))
495+
'''
496+
Traceback (most recent call last):
497+
File "<pyshell#18>", line 1, in <module>
498+
print ("%d " % ("Hello Watermelon"))
499+
TypeError: %d format: a number is required, not str
500+
'''
501+
502+
# print ("%F " % ("Hello Watermelon"))
503+
'''
504+
Traceback (most recent call last):
505+
File "<pyshell#19>", line 1, in <module>
506+
print ("%F " % ("Hello Watermelon"))
507+
TypeError: must be real number, not str
508+
'''
509+
510+
511+
print("\t\t\t\t数据类型必须是整型")
512+
# 做`类型转换`的时候,`十六进制类型`和`八进制类型`需要传入的参数必要是`整型`。
513+
# print ("%X " % ("Hello Watermelon"))
514+
'''
515+
Traceback (most recent call last):
516+
File "<pyshell#11>", line 1, in <module>
517+
print ("%X " % ("Hello Watermelon"))
518+
TypeError: %X format: an integer is required, not str
519+
'''
520+
521+
# print ("%o " % ("Hello Watermelon"))
522+
'''
523+
Traceback (most recent call last):
524+
File "<pyshell#16>", line 1, in <module>
525+
print ("%o " % ("Hello Watermelon"))
526+
TypeError: %o format: an integer is required, not str
527+
'''
528+
529+
530+
print("参数个数错误 --------------------------------")
531+
# `左参数`的`个数`和`右参数`的`个数`一定要相等。
532+
# print ("%s,%s" % ("Hello","Watermelon","!"))
533+
'''
534+
Traceback (most recent call last):
535+
File "<pyshell#29>", line 1, in <module>
536+
print ("%s,%s" % ("Hello","Watermelon","!"))
537+
TypeError: not all arguments converted during string formatting
538+
'''
539+
540+
#------------------------------------------------------------------------------------------------格式化打印错误集合 end
541+
542+
543+
544+
545+
546+
547+
548+
549+
550+
551+
552+
553+
447554

0 commit comments

Comments
 (0)