演示結果: 請輸入用戶名wqe請輸入密碼:wqewqewqerootuser12345689101-100的所有數的和: 50500246810121416182022242628303234363840424446485052545658606264666870727476788082848688 ...
"""
一個用戶登錄的案例
"""
# 永遠等待,直到用戶輸入值
# 變數
name_r = input("請輸入用戶名")
passwd = input("請輸入密碼:")
print(name_r)
print(passwd)
n1 = "root"
n2 = "user"
print(n1)
print(n2)
"""
變數只能有字母、數字、下劃線組成
不能用數字開頭,關鍵字也不能使用['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
不要和python內置的東西重覆
"""
"""
if基本語句
if條件:
內部代碼塊
else:
內部代碼塊
if支持嵌套
if 1==1:
if 2 == 2:
print()
else:
。。。
else:
。。。
if elif
inp = input(“第三方hi”):
if inp == “而無法收到”:
print
elif inp == “第三方”:
pass
elif inp == “第三方hi”:
pass
else:
pass
print()
pass 表示什麼都不執行
"""
"""
8.基本數據類型
字元串(引號)
加法
n1 = “撒打算”
n2 = “撒打算”
n3 = n1 + n2
乘法
n1 = “撒打算”
n2 = n1 * 10
數字
age = 13
加、減、乘、除、取餘、冪
a1 = 10
a2 = 20
a3 = a1 + a2
a4 = a1 - a2
a5 = a1 * a2
a6 = a1 / 10
a7 = a1 % a2 #a1 // a2 取商
a8 = a1 ** a2
判斷奇偶數
a = 13
temp = a % 2
if temp == 0 :
print("o")
else:
print("1")
"""
"""
9 迴圈
while 1== 1 :
print(ok)
"""
# while 輸出 1 2 3 4 5 6 8 9 10
count = 1
while count != 11 :
if count == 7:
pass
else:
print(count)
count = count + 1
# 2、求1-100的所有數的和
numb = 0
temp = 0
while numb <= 100:
temp = numb +temp
numb = numb + 1
print("1-100的所有數的和:",temp)
# 輸出 1-100 內的所有偶數
numb = 0
while numb <= 100:
temp = numb % 2
if temp == 0:
print(numb)
else:
pass
numb = numb + 1
# 輸出 1-100 內的所有奇數
numb = 0
while numb <= 100:
temp = numb % 2
if temp == 0:
pass
else:
print(numb)
numb = numb + 1
# 5、求1-2+3-4+5 ... 99的所有數的和
numb = 0
temp = 0
while numb <= 100:
t = numb % 2
if t == 0:
temp = temp - numb
else:
temp = temp + numb
numb = numb + 1
print("1-100的所有數的和:",temp)
# 6、用戶登陸(三次機會重試)
# user:root passwd :123456
count1 = 0
while count1 < 3:
user_name = input("please write your name:")
passwd = input("your passwd:")
if user_name == 'root' and passwd == '123456' :
print("welcome in")
break
else:
print("error!agenin wirte")
count1 = count1 + 1
# 問題:公雞5文錢一隻,母雞3文錢一隻,小雞3只一文錢,用100文錢買100只雞,其中公雞,母雞,小雞都必須要有,問公雞,母雞,小雞要買多少只剛好湊足100文錢?
"""
5*公雞 < 100
3*母雞 < 100
1*小雞 < 300
公雞+母雞+小雞 = 1000
"""
for g in range(1, 21):
for m in range(1, 34):
for x in range(1, 301):
score = g * 5 + m * 3 + x / 3
if score == 100 and g + m + x == 100:
print('公雞 %s 只,母雞 %s 只,小雞 %s 只' % (g, m, x))
演示結果:
請輸入用戶名wqe
請輸入密碼:wqe
wqe
wqe
root
user
1
2
3
4
5
6
8
9
10
1-100的所有數的和: 5050
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
1-100的所有數的和: -50
please write your name:root
your passwd:qwewqe
error!agenin wirte
please write your name:root
your passwd:123456
welcome in
公雞 4 只,母雞 18 只,小雞 78 只
公雞 8 只,母雞 11 只,小雞 81 只
公雞 12 只,母雞 4 只,小雞 84 只