1 # 彩票系統(與隨機數相同) 2 ''' 3 import random#導入(import) random模塊 4 b = random.randrange(1000,10000) 5 a = int(input("請輸入四位數字:")) 6 if b!=a: 7 print("你沒有中獎,中獎號碼為:",b) 8 else: 9 print("你中獎") 10 ''' 11 12 # 99乘法表 13 ''' 14 a=0 15 while a<=9: 16 b=1 17 while b <= a: 18 print(b,"*",a,"=",a*b," ",end="") 19 b+=1 20 print() 21 a += 1 22 print("*-*-*-*-*-*-*-*--*") 23 24 for i in range(1,10): 25 for j in range(1,i+1): 26 print(j,"*",i,"=",i*j," ",end="") #end橫向排列. 27 print() 28 ''' 29 # i=1 30 # while i<=9:#從一開始到九停 31 # j=1 32 # while j<=i: 33 # print("%d*%d=%d"%(j,i,j*i),end="\t") 34 # j =j+1 35 # print() 36 # i=i+1 37 38 39 40 #從控制台輸入一個數判斷是否是偶數 41 ''' 42 a=int(input("請輸入一個數:")) 43 if a%2==0: 44 print("偶數") 45 else: 46 print("奇數") 47 ''' 48 49 # 從控制台輸入一個數判斷是否是水仙花數 153 == 1**3+5**3+3**3 50 ''' 51 a=int(input("請輸入一個三位數:")) 52 if ((a//100)**3)+((a%100//10)**3)+((a%10)**3)==a: 53 print("是水仙花數") 54 else: 55 print("不是水仙花數") 56 ''' 57 # 從控制台輸入一個數判斷是否是回文數 121 11211 58 59 # a=int(input("請輸入一個數字:")) 60 # b=print(input(~a+1)) 61 62 # 輸入數字使其反向輸出 63 64 # for i in range(7): 65 # if i==5: 66 # break 67 # print(i) 68 # print("循環結束") 69 # 70 71 # for i in range(5): 72 # for j in range(10): 73 # if j==4: 74 # break 75 # print(i,j) 76 # 77 # for i in range(7): 78 # if i ==3: 79 # continue 80 # print(i) 81 # else: 82 # print("end") 83 ''' 84 num = int(input('請輸入一個五位數:')) 85 #獲取第一位 86 #獲取最后一位 87 a = num//10000 88 b=num%10 89 if a==b: 90 #判斷其他兩位 91 c=num//1000%10 92 d=(num%100)//10 93 if c==d: 94 print("是回文數") 95 else: 96 print("不是回文數") 97 else: 98 print("不是回文數") 99 ''' 100 101 102 103 104 ''' 105 # 從控制台輸入兩個個數,輸出較大的值(不能使用max) 106 107 a=int(input("請輸入第一個數字:")) 108 b = int(input("請輸入第一個數字:")) 109 if a>b: 110 print("大的數",a) 111 else: 112 print("小的數",a) 113 ''' 114 ''' 115 # 7、從控制台輸入三個個數,輸出較大的值(不能使用max) 116 a=int(input("請輸入第一個數字:")) 117 b = int(input("請輸入第一個數字:")) 118 c = int(input("請輸入第一個數字:")) 119 if a>b and a>c: 120 print("大的數",a) 121 elif b>c: 122 print("大的數",b) 123 else: 124 print("大的數", c) 125 ''' 126 ''' 127 # 從控制台輸入一個年份,判斷是否為閏年 128 # 能被4整除但是不能被100整除或者能被400整除 129 130 a=int(input("請輸入一個年份:")) 131 if a%4==0 and a%100!=0 or a%400==0: 132 print("閏年") 133 else: 134 print("不是閏年") 135 ''' 136 137 138 # 9、輸入3條邊,判斷是否為三角形,是,就求三角形面積 139 ''' 140 import math 141 a=int(input("請輸入第一條邊:")) 142 b = int(input("請輸入第二條邊:")) 143 c = int(input("請輸入第三條邊:")) 144 if a+b>c and a+c>b and c+b>a: 145 p=(a+b+c)/2 146 mianji=math.sqrt(p*(p-a)*(p-b)*(p-c)) 147 print("三角形") 148 print("面積為",mianji) 149 else: 150 print("不是三角形") 151 ''' 152 153 # 隨機數(搖出相應的隨機數受到相應的懲罰) 154 ''' 155 import random 156 a=random.choice([1,2,3,4]) 157 if a==1: 158 print("被王民陽舔一下") 159 elif a==2: 160 print("被王民陽舔兩下") 161 elif a==3: 162 print("被王民陽舔三下") 163 elif a==4: 164 print("舔三下王民陽") 165 ''' 166 167 ''' 168 # 從1做加法加到一百的和? 169 i =0 170 m=0 171 while i<=100: 172 m=m+i 173 i=i+1 174 print(m) 175 i =0 176 m=0 177 for i in range(1,101): 178 m = m + i 179 i = i + 1 180 print(m) 181 ''' 182 ''' 183 # 1到100所有偶數相加結果(方法1) 184 i =0 185 m=0 186 while i<=100: 187 if i%2==0: 188 m=m+i 189 i=i+1 190 print(m) 191 192 i =0 193 m=0 194 for i in range(1,101): 195 if i % 2 == 0: 196 m = m + i 197 i = i + 1 198 print(m) 199 ''' 200 ''' 201 # 13、把1到100所有3和5的倍數相加 202 i =0 203 m=0 204 while i<=100: 205 if i%3==0 or i%5==0: 206 m=m+i 207 i=i+1 208 print(m) 209 i =0 210 m=0 211 for i in range(1,101): 212 if i%3==0 or i%5==0: 213 m = m + i 214 i = i + 1 215 print(m) 216 ''' 217 # 隨機一個類似手機驗證的四位數字 4 [0123456789] 218 # 用戶在控台輸入四位驗證碼,並進行驗證是否正確 219 # 如: 驗證碼為0123 用戶輸入0123 提示登陸成功,否則登陸失敗 220 ''' 221 import random 222 ran6=int(random.randrange(1000,10000)) 223 print("驗證碼為:",ran6) 224 a =int(input("請輸入驗證碼:")) 225 if a==ran6: 226 print("登錄成功") 227 else: 228 print("登錄失敗") 229 ''' 230 import random 231 res="" 232 for i in range(4): 233 num = random.randint(0,9) 234 res+=str(num) 235 print(res) 236 i=str(input("請輸入驗證碼:")) 237 if i==res: 238 print("ok") 239 else: 240 print("no") 241 242 # 3、給定一個字符串,然后移除制定位置的字符: 243 str1 = "abcdefgh" 244 index = int(input("請輸入您想刪除的下標(下標從0開始)")) 245 strRes = "" 246 for i in range(len(str1)): 247 if index == i: 248 continue 249 strRes += str1[i] 250 print(str1) 251 print(strRes) 252 253 # [::] 254 # replace 255 # strip 256 257 258 count = 0 259 for i in str1: 260 count += 1 261 print(count) 262 263 264 str2 = "sf sdf gw ewag r" 265 count = 1 266 for i in str2: 267 if i.isspace(): 268 count += 1 269 print(count) 270 271 272 # ad1sfjei45sdjg8 273 str3 = "ad1sfjei45sdjg8" 274 sumRes = 0 275 for i in str3: 276 # if i.isdigit(): 277 # sumRes += int(i) 278 if i >= "0" and i <= "9": 279 sumRes += int(i) 280 print(sumRes) 281 282 283 for i in range(1,10): 284 strRes = "" 285 for j in range(1, i + 1): # [1,2,3,4,5,6,7,8,9] 286 strRes += str(j) 287 # print(j, end="") 288 print(strRes) 289 290 291 for i in range(ord("A"), ord("Z") + 1): 292 strRes = "" 293 for j in range(ord("A"), i + 1): # [1,2,3,4,5,6,7,8,9] 294 strRes += chr(j) 295 # print(j, end="") 296 print(strRes) 297 298 str1 = "qwe 1 12345 gfd SDFGH *&^%$# 98765" 299 count1 = 0 # 數字 300 count2 = 0 # 空格 301 count3 = 0 # 字母 302 count4 = 0 # 其他 303 for i in str1: 304 if i >= "0" and i <= "9": 305 count1 += 1 306 elif i >= "a" and i <= "z": 307 count3 += 1 308 elif i >= "A" and i <= "Z": 309 count3 += 1 310 elif i == " ": 311 count2 += 1 312 else: 313 count4 += 1 314 315 316 n = 10 317 sumRes = 0 318 for i in range(n): 319 sumRes += 2**i 320 # print(2**i) 321 print(sumRes) 322 323 324 325 326 a = input("123:") 327 b = input("123:") 328 print("字符串%s和%s" % (a, b))
