Python3 typical small example and garbled problem

1. Example 1:

Question: Input a line of string, and count the number of English letters, spaces, numbers and other characters in it.

1. Program analysis: Using the while statement, the condition is that the input character is not '\n'. #Use isdigit function to judge whether it is a number #Use isalpha to judge whether it is a letter #isalnum judge whether it is a combination of a number and a letter.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/10 22:13
# @Author  : Feng Xiaoqing
# @File    : string.py
# @Function: -----------

num_dig=0
num_alph=0
num_space=0
num_other=0

while 1:
    strings=input("Please input string:")
    if strings=="quit":
        exit(0)
    for i in strings:
        if i.strip().isdigit():
            num_dig += 1
        elif i.strip().isalpha():
            num_alph += 1
        elif i.isspace():
            num_space += 1
        else:
            num_other += 1
    print("数字个数为:{0}".format(num_dig))
    print("字母个数为:{0}".format(num_alph))
    print("空格个数为:{0}".format(num_space))
    print("其他字符个数为:{0}".format(num_other))

operation result:

Please input string:kadsf83543k#### asdflka
数字个数为:5
字母个数为:13
空格个数为:1
其他字符个数为:4

 

2. Example 2:

Question: Write a code to input a number and automatically calculate the factorial of this number.

That is, n!=1×2×3×...×n. Factorials can also be defined recursively : 0!=1, n!=(n-1)!×n.

Such as:

0! = 1
1! = 1
2! = 2*1=2
3! = 3*2*1=6

code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/11 22:48
# @Author  : Feng Xiaoqing
# @File    : 4.JieCheng_for_n.py
# @Function: ----------

n=input("Please input a number n: ")
result = 1
num = 1
for i in range(1,int(n)+1):
    num *=i
    result += num
print("{0}的阶乘为:{1}".format(n,result))



另一种:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/10 21:59
# @Author  : Feng Xiaoqing
# @File    : jiecheng_for_n.py
# @Function: -----------

def jc(n):
    result = 1
    if n == 0:
        return result
    else:
        for i in range(1,n+1):
            result *= i
        return result
n = input("Please input number n:")
count = 0
for i in range(0,int(n)+1):
    count += jc(i)

print("count = {0}".format(count))

Operation result:

Please input a number n: 8
8的阶乘为:46234

另一种:
Please input number n:8
count = 46234

 

3. Example three:

Question: ABCD times 9=DCBA, A=? B=? C=? D=? What are the values ​​of A, B, C, and D?

Answer: a=1, b=0, c=8, d=9 1089*9=9801

code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/11 22:14
# @Author  : Feng Xiaoqing
# @File    : 2.ABCDX9=DCBA.py
# @Function: -----------

for A in range(1,10):
    for B in range(0,10):
        for C in range(0,10):
            for D in range(0,10):
                if (A*1000+B*100+C*10+D)*9==D*1000+C*100+B*10+A :
                    print("A={0}".format(A))
                    print("B={0}".format(B))
                    print("C={0}".format(C))
                    print("D={0}".format(D))
                    print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(A,B,C,D))

operation result:

A=1
B=0
C=8
D=9
1089x9=9801


 

4. Example four:

Topic: Write a code to realize the calculation of the nine-square grid Sudoku.

               -------------
                | A | B | C |
                | D | E | F |
                | G | H | I |
                -------------

A,B,C,D,E,F,G,H,I are 1-9
and all the horizontal and vertical slashes add up to 15:

(A+B+C)=(D+E+F)=(G+H+I)=(A+D+G)=(B+E+H)=(C+F+I)=(A+E+I)=(G+E+C)= 15

code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/11 23:31
# @Author  : Feng Xiaoqing
# @File    : jiu_Gong_Ge.py
# @Function: -----------

count=0
list=[0,1,2,3,4,5,6,7,8,9]
for A in list:
    a=list.copy()
    a.remove(A)
    for B in a:
        b = a.copy()
        b.remove(B)
        for C in b:
            c = b.copy()
            c.remove(C)
            for D in c:
                d= c.copy()
                d.remove(D)
                for E in d:
                    e = d.copy()
                    e.remove(E)
                    for F in e:
                        f = e.copy()
                        f.remove(F)
                        for G in f:
                            g = f.copy()
                            g.remove(G)
                            for H in g:
                                h = g.copy()
                                h.remove(H)
                                for I in h:
                                    if (A+B+C)==(D+E+F)==(G+H+I)==(A+D+G)==(B+E+H)==(C+F+I)==(A+E+I)==(G+E+C)== 15:
                                        count += 1
                                        print("""                                       
                                                        ---第{9}种----
                                                        | {0} | {1} | {2} |
                                                        | {3} | {4} | {5} |
                                                        | {6} | {7} | {8} |
                                                        -------------                                                                               
                                        """.format(A,B,C,D,E,F,G,H,I,count))

operation result:

                                                        ---第1种----
                                                        | 2 | 7 | 6 |
                                                        | 9 | 5 | 1 |
                                                        | 4 | 3 | 8 |
                                                        -------------                                                                               
                                        
                                       
                                                        ---第2种----
                                                        | 2 | 9 | 4 |
                                                        | 7 | 5 | 3 |
                                                        | 6 | 1 | 8 |
                                                        -------------                                                                               
                                        
                                       
                                                        ---第3种----
                                                        | 4 | 3 | 8 |
                                                        | 9 | 5 | 1 |
                                                        | 2 | 7 | 6 |
                                                        -------------                                                                               
                                        
                                       
                                                        ---第4种----
                                                        | 4 | 9 | 2 |
                                                        | 3 | 5 | 7 |
                                                        | 8 | 1 | 6 |
                                                        -------------                                                                               
                                        
                                       
                                                        ---第5种----
                                                        | 6 | 1 | 8 |
                                                        | 7 | 5 | 3 |
                                                        | 2 | 9 | 4 |
                                                        -------------                                                                               
                                        
                                       
                                                        ---第6种----
                                                        | 6 | 7 | 2 |
                                                        | 1 | 5 | 9 |
                                                        | 8 | 3 | 4 |
                                                        -------------                                                                               
                                        
                                       
                                                        ---第7种----
                                                        | 8 | 1 | 6 |
                                                        | 3 | 5 | 7 |
                                                        | 4 | 9 | 2 |
                                                        -------------                                                                               
                                        
                                       
                                                        ---第8种----
                                                        | 8 | 3 | 4 |
                                                        | 1 | 5 | 9 |
                                                        | 6 | 7 | 2 |
                                                        -------------                                                                               
                                        

Encoding issues in python:

1. There will be no garbled characters in the utf-8 Pycharm program and in the cmd.

2. When it is gbk, there are no garbled characters

 

3. The file states that it is utf-8 encoding. After identifying "Hello", it exists in the form of a unicode object. If we use type to view, the storage form is unicode, and python will automatically convert it according to the encoding of the output environment when outputting unicode objects to the console. If the output is not a unicode object but a str type. The string will be output according to the encoding of the string. As a result, utf8 cannot be displayed on the gbk-encoded console

4. In the list, there are garbled characters in the console in Python2, no garbled characters in cmd, and no garbled characters in python3:

In python3:

5. Encoding conversion error: there are garbled characters in the console in python2, no garbled characters in cmd, and no garbled characters in python3:

python3:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324416073&siteId=291194637