标签:
Python版本:python 3.2.2
电脑系统:win7旗舰
实例来源:python菜鸟教程100例
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 import string 4 import math 5 import time 6 import sys 7 import os 8 #import pygame 9 #eg1:There are 1, 2, 3, 4 numbers, can be composed of a number of different and no duplication of the three digit number? How much is it? 10 for i in range(1,5): 11 for j in range(1,5): 12 for k in range(1,5): 13 if (i != k) and (i != j) and (j != k): 14 print("The number is ",i,j,k) 15 #eg2:Bonuses paid by enterprises in accordance with the profit commission. Total number of 1.5% between 7.5% of the profits (I) less than or equal to 10 million yuan, bonus provided 10%; profit of more than 10 million yuan, less than 20 million yuan, less than 10 million yuan in 10% deduct a percentage from a sum of money, more than 10 million yuan, the Commission; 20 million to 40 million, more than 20 million yuan, to the Commission of 5%; 40 million to 60 million more than 40 million yuan, to the Commission of 3%; 60 million to 100 million, more than 60 million yuan, deduct a percentage from a sum of money, more than 100 million yuan, more than 100 million yuan according to the 1% commission, from the keyboard input month profit I, seeking to be bonuses? 16 #Solution 1 17 percent_less10mili = 0.1 18 percent_less20mili = 0.075 19 percent_less40mili = 0.05 20 percent_less60mili = 0.03 21 percent_less100mili = 0.015 22 percent_last = 0.01 23 I=input("Please enter a the profit for this year :") 24 i=int(I) 25 if i <= 10: 26 Sum = i*percent_less10mili 27 elif 10 < i and i <= 20: 28 Sum = 1+(i-10)*percent_less20mili 29 elif 20 < i and i <= 40: 30 Sum = 1+0.75+(i-20)*percent_less40mili 31 elif 40 < i and i <= 60: 32 Sum = 1+0.75+1+(i-40)*percent_less60mili 33 elif 60 < i and i <= 100: 34 Sum = 1+0.75+1+0.6+(i-60)*percent_less100mili 35 elif 100 < i: 36 Sum = 1+0.75+1+0.6+0.6+(i-100)*percent_last 37 else : 38 print("Your have enter a wrong number!") 39 print("The profit of this year for MaMiao",Sum) 40 #Solution 2 41 i = int(input("The profit:")) 42 arr = [1000000,600000,400000,200000,100000,0] 43 rat = [0.01,0.015,0.03,0.05,0.075,0.1] 44 r = 0 45 for idx in range(0,6): 46 if i>arr[idx]: 47 r+=(i-arr[idx])*rat[idx] 48 print((i-arr[idx])*rat[idx]) 49 i=arr[idx] 50 print(r) 51 #eg3:An integer, which adds 100 and plus 268 is a perfect square, what is the number? 52 #Solution 1 53 n=0 54 m=0 55 for K_f in range(1,12):#This algorithm has a simple mathematical pretreatment and analysis, to a certain extent, the time complexity of the algorithm is simplified. 56 K_s = 168/K_f 57 if (K_s == int(K_s)): 58 m=(K_s+K_f)/2 59 n=(K_s-K_f)/2 60 if n == int(n) and m == int(m) and n!=0 and m!=0: 61 print(m,n) 62 print(m*m-268) 63 #Solution 2 64 for i in range(10000): 65 x = int(math.sqrt(i + 100)) 66 y = int(math.sqrt(i + 268)) 67 if(x * x == i + 100) and (y * y == i + 268): 68 print(i) 69 #eg4:Enter a certain day, judgment day is the first few days this year? 70 year = int(input("Please enter the year:")) 71 month = int(input("Please enter the month:")) 72 day = int(input("Please enter the day:")) 73 sum = 0 74 month_day=(31,28,31,30,31,30,31,31,30,31,30,31) 75 if (year%4==0 and year%100!=0) or year%400==0 : 76 day_plus = 1 77 print("This year is a leap year!") 78 else : 79 day_plus = 0 80 print("This year is not a leap year!") 81 if 2 < month : 82 sum += day_plus 83 for i in range(0,month-1): 84 sum += month_day[i] 85 sum += day 86 print("The sum of today is equal to:",sum) 87 #eg5:Enter three integers x, y, z, please put the three number of small to large output. 88 #Solution 1 89 def compare(A,B): 90 if A>B: 91 result = A 92 else: 93 result = B 94 return result 95 NUM1=int(input("Please enter the first number:")) 96 NUM2=int(input("Please enter the second number:")) 97 NUM3=int(input("Please enter the third number:")) 98 Big1=compare(NUM1,NUM2) 99 Big2=compare(Big1,NUM3)#caculate the biggest one 100 if NUM1 == Big2: 101 NUM1=0 102 if NUM2 == Big2: 103 NUM2=0 104 if NUM3 == Big2: 105 NUM3=0 106 middle1=compare(NUM1,NUM2) 107 middle2=compare(middle1,NUM3)#caculate the second biggest one 108 if NUM1 == middle2: 109 NUM1=0 110 if NUM2 == middle2: 111 NUM2=0 112 if NUM3 == middle2: 113 NUM3=0 114 small1=compare(NUM1,NUM2) 115 small2=compare(small1,NUM3)#caculate the second biggest one 116 print(small2,middle2,Big2) 117 #Solution 2 118 l = [] 119 for i in range(3): 120 x = int(input("Please enter the first number:")) 121 l.append(x) 122 l.sort() 123 print(l) 124 #eg6:Fibonacci sequence 125 #Solution 1 126 F = [0,1,1] 127 print(F[0]) 128 print(F[1]) 129 for i in range(1,10): 130 F[0]=F[1] 131 F[1]=F[2] 132 F[2]=F[0]+F[1] 133 print(F[2]) 134 #Solution 2 135 def fib(n): 136 if n==1 or n==2: 137 return 1 138 return fib(n-1)+fib(n-2) 139 print(fib(10)) 140 #Copy a list of data into another list. 141 #Solution 1 142 a = [1, 2, 3] 143 b = a[:] 144 print(b) 145 #Solution 2 146 c=[0,0,0] 147 for i in range(0,3): 148 c[i]=a[i] 149 print(c) 150 #eg8:The output of 9*9 multiplication table 151 for i in range(1,10): 152 for j in range(1,10): 153 print(i,"*",j,"=",i*j) 154 #eg9:Pause one second output 155 #It need to import the headfiles named time like this:import time 156 myD = {1: ‘a‘, 2: ‘b‘} 157 for key,value in dict.items(myD): 158 print(key, value) 159 print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))) 160 time.sleep(1) 161 print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))) 162 #To determine how many prime numbers between 101-200, and the output of all prime numbers 163 h = 0 164 leap = 1 165 from math import sqrt 166 from sys import stdout 167 for m in range(101,201): 168 k = int(sqrt(m + 1)) 169 for i in range(2,k + 1): 170 if m % i == 0: 171 leap = 0 172 break 173 if leap == 1: 174 print(‘%-4d‘ % m) 175 h += 1 176 if h % 10 == 0: 177 print(‘‘) 178 leap = 1 179 print(‘The total is %d‘ % h) 180 #eg10:Print out all the "daffodils", the so-called "daffodils" is a three digit number. The all digital cube and equal to the number itself. For example: 153 is a "daffodils", because 153=1^3+5^3+3^3 181 for i in range(100,1000): 182 sum = math.pow(int(i/100),3)+ math.pow(int((i%100)/10),3)+ math.pow(i%10,3) 183 if sum==i: 184 print("This number is a daffodils number equal to:",i) 185 #eg11:A positive integer factorization. For example: enter 90, print out 90=2*3*3*5 186 n = int(input("input number:\n")) 187 print("n = %d" % n) 188 for i in range(2,n + 1): 189 while n != i: 190 if n % i == 0:#All the number,which is not a prime number can‘t appear at there,because that the number(not prime number) have ever been diverse to be the premi number ,for Example:10 have already been diverse to be 2 multiple 5 191 print(str(i)) 192 print("*") 193 n = n / i 194 else: 195 break 196 print("%d"% n) 197 #eg11:Use the conditional operator to complete this problem: the study results >=90 points of the students with A, 60-89 points between the use of B, said the following 60 points with C 198 Score = int(input("Please enter the score:"))#Python haven‘t the loop function for three member like this : A = expression ? "Q" : "P" 199 if Score < 60: 200 print("Your Grade is equal to C") 201 elif 60<= Score < 90: 202 print("Your Grade is equal to B") 203 else : 204 print("Your Grade is equal to A") 205 #eg12:Enter a line of characters, respectively, the statistics of the English letters, spaces, numbers and other characters of the number 206 write = "Come on,Baby!" 207 write = write.encode() 208 fo = open("eg_test.txt",‘wb‘,1000) 209 print("The filename is :",fo.name) 210 print("The Rquest_Model of files :",fo.mode) 211 print("This is the sentence which I have already written into the file named ",fo.name,write) 212 fo.write(write) 213 fo.close() 214 fo = open("eg_test.txt","rb",1) 215 Str = fo.read(15) 216 Str = Str.decode()#You have to decode the string to count how many times the letter "C" have appear in the str! 217 print("The string i have already read from txt file is :",Str) 218 C = Str.count(‘C‘) 219 Where = Str.find("Ba") 220 print("The latter C have appear for ",C,"times!") 221 print("The string om have appear at the address:",Where) 222 fo.close() 223 letters = 0 224 space = 0 225 digit = 0 226 others = 0 227 for c in Str: 228 if c.isalpha():#remember the function to find alpha 229 letters += 1 230 elif c.isspace():#rememeber the function to find the space 231 space += 1 232 elif c.isdigit():#remember the function to find the digit 233 digit += 1 234 else: 235 others += 1 236 print(‘char = %d,space = %d,digit = %d,others = %d‘ % (letters,space,digit,others))#输出 237 #eg13:Detect keyboard to control the size of the key 238 print("Please enter the keyboard values,this example is used to detect the values of top bottom right and left values of keyboard!") 239 top = input("Please enter the top key in keyboard!") 240 #bottom = input("Please enter the bottom key in keyboard!") 241 #left = input("Please enter the left key in keyboard!") 242 #right = input("Please enter the right key in keyboard!") 243 print("The top values is equal to ",top,int(top)) 244 #print("The bottom values is equal to ",bottom,int(bottom)) 245 #print("The left values is equal to ",left,int(left)) 246 #print("The right values is equal to ",right,int(right)) 247 #eg14:Find the value of s=a+aa+aaa+aaaa+aa... A, where a is a number. For example, 2+22+222+2222+22222 (at this time a total of 5 numbers add), a few numbers add a keyboard control 248 K = int(input("Please enter a Number to calculate:")) 249 N = int(input("Please enter a Number to define how many time you want to calculate:")) 250 #for i in range(1,10):#范围是1到10-1=9,记住了!!! 251 # print(i) 252 #for i in range(10):#范围是0到9之间,记住了!!! 253 # print(i) 254 sum = 0#出初始化一定要做到啊!!!! 255 for i in range(1,N+1): 256 temp = i*math.pow(10,N-i) 257 sum += temp 258 print("The precudure is :",temp,"+") 259 print(sum) 260 print("The result is calculate to be :",sum*K) 261 #eg15:If a number is exactly equal to the sum of its factors, this number is called "end of a few". For example 6=123. programming to find all completed within 1000 262 i=1 263 while (i<=1000): 264 Buffer = i 265 for j in range(1,i): 266 if (i%j==0): 267 Buffer -= j 268 if (Buffer==0): 269 print("This number is a complete number equal to:",i) 270 i += 1 271 #eg16:A ball dropped from a height of 100 m free. Each fall to the ground after jump back to the original height of half; to fall for it on the 10th floor, the total number of meters after? How high is the tenth bounce? 272 Heigth = int(input("Please enter a Number for Heigth:")) 273 sum = Heigth 274 Times = int(input("Please enter a Number for calculate times:")) 275 print("The Heigth of 10th bounce is equal to :",math.pow(0.5,Times)*Heigth) 276 for i in range(1,Times): 277 sum += 2*Heigth*math.pow(0.5,i) 278 print("The total length is equal to:",sum) 279 #eg17:Two table tennis team for the game, each out of the three. A team for the A, B, C three, B team for the X, y, Z three. Draw a draw. Someone asked the team about the competition. A said he did not x than, C said he does not and Z, X than, please make up the program to find out the list of three teams race 280 for i in range(ord(‘x‘),ord(‘z‘) + 1): 281 for j in range(ord(‘x‘),ord(‘z‘) + 1): 282 if i != j: 283 for k in range(ord(‘x‘),ord(‘z‘) + 1): 284 if (i != k) and (j != k): 285 if (i != ord(‘x‘)) and (k != ord(‘x‘)) and (k != ord(‘z‘)): 286 print(‘order is a -- %s\t b -- %s\tc--%s‘ % (chr(i),chr(j),chr(k))) 287 #这种题目有很强的抽象性,一定要注意求解的方法 288 #eg8:Print out the following pattern (diamond) 289 print(" *") 290 print(" ***") 291 print(" *****") 292 print("*******") 293 print(" *****") 294 print(" ***") 295 print(" *") 296 #eg19:There is a sequence of points: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13, and the sum of the first 20 items of this series. 297 #分析题目的基本规律: 298 #分子:2+3=5 3+5=8 5+8=13 8+13=21 299 #分母:1+2=3 2+3=5 3+5=8 5+8=13 300 Top_num1=2 301 Bot_num1=1 302 Top_num2=3 303 Bot_num2=2 304 sum=7/2 305 for i in range(18): 306 #Num = (Top_num1+Top_num2)/(Bot_num1+Bot_num2) 307 print("The number is :",Top_num1,Bot_num1) 308 Temp1=Top_num1#中间变量的申请和数据更新及存放注意啊!! 309 Temp2=Bot_num1 310 Top_num1 = Top_num2 311 Bot_num1 = Bot_num2 312 Top_num2 = (Temp1+Top_num2) 313 Bot_num2 = (Temp2+Bot_num2) 314 sum += Top_num2/Bot_num2 315 print(sum) 316 #eg20:Use recursive method for 5! 317 def recursive(N): 318 result = N 319 if (N==1): 320 return 1 321 else : 322 result *= recursive(N-1) 323 return result 324 result = recursive(5) 325 print(result) 326 #eg21:By using the recursive function call, the input of the 5 characters, in order to print out the opposite order 327 #Solution 1: 328 FIFO=[‘A‘,‘B‘,‘C‘,‘D‘,‘E‘] 329 print("Please enter five letter for FIFO!") 330 for i in range(5): 331 FIFO[i] = input("Please enter the letter:") 332 print(FIFO[0],FIFO[1],FIFO[2],FIFO[3],FIFO[4]) 333 print(FIFO[4],FIFO[3],FIFO[2],FIFO[1],FIFO[0]) 334 print(FIFO[4]+FIFO[3]+FIFO[2]+FIFO[1]+FIFO[0]) 335 #Solution 2(采用递归算法): 336 def output(s,l): 337 if l==0: 338 return 339 print(s[l-1]) 340 output(s,l-1) 341 s = FIFO[4]+FIFO[3]+FIFO[2]+FIFO[1]+FIFO[0] 342 l = len(s) 343 output(s,l) 344 #eg22:A 5 digit number, it is not a palindrome judgment. That 12321 is a palindrome, a bit and bit the same ten million, with thousands of the same 345 346 #del Num 347 Num = 12321 348 print("The number for test is :",Num) 349 if (int(Num/10000)==Num%10) and (int(Num/1000)%10==int((Num%100)/10)): 350 print("This is a palindrome number!") 351 #eg23:Please enter the first letter of the week to determine what is a week, if the first letter, then continue to determine the second letters 352 days = [‘Monday‘,‘Thuesday‘,‘Wednesday‘,‘Thursday‘,‘Friday‘,‘Saturday‘,‘Sunday‘] 353 print("Please enter the first letter to determine the day in week:") 354 Ch1=input("Please enter the first letter:") 355 if Ch1==‘M‘: 356 print("Today is Monday!") 357 elif Ch1==‘W‘: 358 print("Today is Wednsday!") 359 elif Ch1==‘F‘: 360 print("Today is Friday!") 361 elif Ch1==‘T‘: 362 print("Please enter the second letter for determined!") 363 Ch2=input() 364 if Ch2==‘h‘: 365 print("Today is Thuesday!") 366 else : 367 print("Today is Tursday!") 368 else: 369 print("Please enter the second letter for detemined!") 370 Ch2=input() 371 if Ch2==‘a‘: 372 print("Today is Saturday!") 373 else : 374 print("Today is Sunday!") 375 #eg24:Comma separated list按逗号分隔列表 376 L = [1,2,3,4,5] 377 s1 = ‘,‘.join(str(n) for n in L) 378 print(s1) 379 #eg25:文本颜色设置--示例35 380 class bcolors: 381 HEADER = ‘\033[95m‘ 382 OKBLUE = ‘\033[94m‘ 383 OKGREEN = ‘\033[92m‘ 384 WARNING = ‘\033[93m‘ 385 FAIL = ‘\033[91m‘ 386 ENDC = ‘\033[0m‘ 387 BOLD = ‘\033[1m‘ 388 UNDERLINE = ‘\033[4m‘ 389 print(bcolors.WARNING + "Warning mesg color?" + bcolors.ENDC)
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 import string 4 import math 5 import time 6 import sys 7 import os 8 #import pygame 9 #eg1:There are 1, 2, 3, 4 numbers, can be composed of a number of different and no duplication of the three digit number? How much is it? 10 for i in range(1,5): 11 for j in range(1,5): 12 for k in range(1,5): 13 if (i != k) and (i != j) and (j != k): 14 print("The number is ",i,j,k) 15 #eg2:Bonuses paid by enterprises in accordance with the profit commission. Total number of 1.5% between 7.5% of the profits (I) less than or equal to 10 million yuan, bonus provided 10%; profit of more than 10 million yuan, less than 20 million yuan, less than 10 million yuan in 10% deduct a percentage from a sum of money, more than 10 million yuan, the Commission; 20 million to 40 million, more than 20 million yuan, to the Commission of 5%; 40 million to 60 million more than 40 million yuan, to the Commission of 3%; 60 million to 100 million, more than 60 million yuan, deduct a percentage from a sum of money, more than 100 million yuan, more than 100 million yuan according to the 1% commission, from the keyboard input month profit I, seeking to be bonuses? 16 #Solution 1 17 percent_less10mili = 0.1 18 percent_less20mili = 0.075 19 percent_less40mili = 0.05 20 percent_less60mili = 0.03 21 percent_less100mili = 0.015 22 percent_last = 0.01 23 I=input("Please enter a the profit for this year :") 24 i=int(I) 25 if i <= 10: 26 Sum = i*percent_less10mili 27 elif 10 < i and i <= 20: 28 Sum = 1+(i-10)*percent_less20mili 29 elif 20 < i and i <= 40: 30 Sum = 1+0.75+(i-20)*percent_less40mili 31 elif 40 < i and i <= 60: 32 Sum = 1+0.75+1+(i-40)*percent_less60mili 33 elif 60 < i and i <= 100: 34 Sum = 1+0.75+1+0.6+(i-60)*percent_less100mili 35 elif 100 < i: 36 Sum = 1+0.75+1+0.6+0.6+(i-100)*percent_last 37 else : 38 print("Your have enter a wrong number!") 39 print("The profit of this year for MaMiao",Sum) 40 #Solution 2 41 i = int(input("The profit:")) 42 arr = [1000000,600000,400000,200000,100000,0] 43 rat = [0.01,0.015,0.03,0.05,0.075,0.1] 44 r = 0 45 for idx in range(0,6): 46 if i>arr[idx]: 47 r+=(i-arr[idx])*rat[idx] 48 print((i-arr[idx])*rat[idx]) 49 i=arr[idx] 50 print(r) 51 #eg3:An integer, which adds 100 and plus 268 is a perfect square, what is the number? 52 #Solution 1 53 n=0 54 m=0 55 for K_f in range(1,12):#This algorithm has a simple mathematical pretreatment and analysis, to a certain extent, the time complexity of the algorithm is simplified. 56 K_s = 168/K_f 57 if (K_s == int(K_s)): 58 m=(K_s+K_f)/2 59 n=(K_s-K_f)/2 60 if n == int(n) and m == int(m) and n!=0 and m!=0: 61 print(m,n) 62 print(m*m-268) 63 #Solution 2 64 for i in range(10000): 65 x = int(math.sqrt(i + 100)) 66 y = int(math.sqrt(i + 268)) 67 if(x * x == i + 100) and (y * y == i + 268): 68 print(i) 69 #eg4:Enter a certain day, judgment day is the first few days this year? 70 year = int(input("Please enter the year:")) 71 month = int(input("Please enter the month:")) 72 day = int(input("Please enter the day:")) 73 sum = 0 74 month_day=(31,28,31,30,31,30,31,31,30,31,30,31) 75 if (year%4==0 and year%100!=0) or year%400==0 : 76 day_plus = 1 77 print("This year is a leap year!") 78 else : 79 day_plus = 0 80 print("This year is not a leap year!") 81 if 2 < month : 82 sum += day_plus 83 for i in range(0,month-1): 84 sum += month_day[i] 85 sum += day 86 print("The sum of today is equal to:",sum) 87 #eg5:Enter three integers x, y, z, please put the three number of small to large output. 88 #Solution 1 89 def compare(A,B): 90 if A>B: 91 result = A 92 else: 93 result = B 94 return result 95 NUM1=int(input("Please enter the first number:")) 96 NUM2=int(input("Please enter the second number:")) 97 NUM3=int(input("Please enter the third number:")) 98 Big1=compare(NUM1,NUM2) 99 Big2=compare(Big1,NUM3)#caculate the biggest one 100 if NUM1 == Big2: 101 NUM1=0 102 if NUM2 == Big2: 103 NUM2=0 104 if NUM3 == Big2: 105 NUM3=0 106 middle1=compare(NUM1,NUM2) 107 middle2=compare(middle1,NUM3)#caculate the second biggest one 108 if NUM1 == middle2: 109 NUM1=0 110 if NUM2 == middle2: 111 NUM2=0 112 if NUM3 == middle2: 113 NUM3=0 114 small1=compare(NUM1,NUM2) 115 small2=compare(small1,NUM3)#caculate the second biggest one 116 print(small2,middle2,Big2) 117 #Solution 2 118 l = [] 119 for i in range(3): 120 x = int(input("Please enter the first number:")) 121 l.append(x) 122 l.sort() 123 print(l) 124 #eg6:Fibonacci sequence 125 #Solution 1 126 F = [0,1,1] 127 print(F[0]) 128 print(F[1]) 129 for i in range(1,10): 130 F[0]=F[1] 131 F[1]=F[2] 132 F[2]=F[0]+F[1] 133 print(F[2]) 134 #Solution 2 135 def fib(n): 136 if n==1 or n==2: 137 return 1 138 return fib(n-1)+fib(n-2) 139 print(fib(10)) 140 #Copy a list of data into another list. 141 #Solution 1 142 a = [1, 2, 3] 143 b = a[:] 144 print(b) 145 #Solution 2 146 c=[0,0,0] 147 for i in range(0,3): 148 c[i]=a[i] 149 print(c) 150 #eg8:The output of 9*9 multiplication table 151 for i in range(1,10): 152 for j in range(1,10): 153 print(i,"*",j,"=",i*j) 154 #eg9:Pause one second output 155 #It need to import the headfiles named time like this:import time 156 myD = {1: ‘a‘, 2: ‘b‘} 157 for key,value in dict.items(myD): 158 print(key, value) 159 print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))) 160 time.sleep(1) 161 print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))) 162 #To determine how many prime numbers between 101-200, and the output of all prime numbers 163 h = 0 164 leap = 1 165 from math import sqrt 166 from sys import stdout 167 for m in range(101,201): 168 k = int(sqrt(m + 1)) 169 for i in range(2,k + 1): 170 if m % i == 0: 171 leap = 0 172 break 173 if leap == 1: 174 print(‘%-4d‘ % m) 175 h += 1 176 if h % 10 == 0: 177 print(‘‘) 178 leap = 1 179 print(‘The total is %d‘ % h) 180 #eg10:Print out all the "daffodils", the so-called "daffodils" is a three digit number. The all digital cube and equal to the number itself. For example: 153 is a "daffodils", because 153=1^3+5^3+3^3 181 for i in range(100,1000): 182 sum = math.pow(int(i/100),3)+ math.pow(int((i%100)/10),3)+ math.pow(i%10,3) 183 if sum==i: 184 print("This number is a daffodils number equal to:",i) 185 #eg11:A positive integer factorization. For example: enter 90, print out 90=2*3*3*5 186 n = int(input("input number:\n")) 187 print("n = %d" % n) 188 for i in range(2,n + 1): 189 while n != i: 190 if n % i == 0:#All the number,which is not a prime number can‘t appear at there,because that the number(not prime number) have ever been diverse to be the premi number ,for Example:10 have already been diverse to be 2 multiple 5 191 print(str(i)) 192 print("*") 193 n = n / i 194 else: 195 break 196 print("%d"% n) 197 #eg11:Use the conditional operator to complete this problem: the study results >=90 points of the students with A, 60-89 points between the use of B, said the following 60 points with C 198 Score = int(input("Please enter the score:"))#Python haven‘t the loop function for three member like this : A = expression ? "Q" : "P" 199 if Score < 60: 200 print("Your Grade is equal to C") 201 elif 60<= Score < 90: 202 print("Your Grade is equal to B") 203 else : 204 print("Your Grade is equal to A") 205 #eg12:Enter a line of characters, respectively, the statistics of the English letters, spaces, numbers and other characters of the number 206 write = "Come on,Baby!" 207 write = write.encode() 208 fo = open("eg_test.txt",‘wb‘,1000) 209 print("The filename is :",fo.name) 210 print("The Rquest_Model of files :",fo.mode) 211 print("This is the sentence which I have already written into the file named ",fo.name,write) 212 fo.write(write) 213 fo.close() 214 fo = open("eg_test.txt","rb",1) 215 Str = fo.read(15) 216 Str = Str.decode()#You have to decode the string to count how many times the letter "C" have appear in the str! 217 print("The string i have already read from txt file is :",Str) 218 C = Str.count(‘C‘) 219 Where = Str.find("Ba") 220 print("The latter C have appear for ",C,"times!") 221 print("The string om have appear at the address:",Where) 222 fo.close() 223 letters = 0 224 space = 0 225 digit = 0 226 others = 0 227 for c in Str: 228 if c.isalpha():#remember the function to find alpha 229 letters += 1 230 elif c.isspace():#rememeber the function to find the space 231 space += 1 232 elif c.isdigit():#remember the function to find the digit 233 digit += 1 234 else: 235 others += 1 236 print(‘char = %d,space = %d,digit = %d,others = %d‘ % (letters,space,digit,others))#输出 237 #eg13:Detect keyboard to control the size of the key 238 print("Please enter the keyboard values,this example is used to detect the values of top bottom right and left values of keyboard!") 239 top = input("Please enter the top key in keyboard!") 240 #bottom = input("Please enter the bottom key in keyboard!") 241 #left = input("Please enter the left key in keyboard!") 242 #right = input("Please enter the right key in keyboard!") 243 print("The top values is equal to ",top,int(top)) 244 #print("The bottom values is equal to ",bottom,int(bottom)) 245 #print("The left values is equal to ",left,int(left)) 246 #print("The right values is equal to ",right,int(right)) 247 #eg14:Find the value of s=a+aa+aaa+aaaa+aa... A, where a is a number. For example, 2+22+222+2222+22222 (at this time a total of 5 numbers add), a few numbers add a keyboard control 248 K = int(input("Please enter a Number to calculate:")) 249 N = int(input("Please enter a Number to define how many time you want to calculate:")) 250 #for i in range(1,10):#范围是1到10-1=9,记住了!!! 251 # print(i) 252 #for i in range(10):#范围是0到9之间,记住了!!! 253 # print(i) 254 sum = 0#出初始化一定要做到啊!!!! 255 for i in range(1,N+1): 256 temp = i*math.pow(10,N-i) 257 sum += temp 258 print("The precudure is :",temp,"+") 259 print(sum) 260 print("The result is calculate to be :",sum*K) 261 #eg15:If a number is exactly equal to the sum of its factors, this number is called "end of a few". For example 6=123. programming to find all completed within 1000 262 i=1 263 while (i<=1000): 264 Buffer = i 265 for j in range(1,i): 266 if (i%j==0): 267 Buffer -= j 268 if (Buffer==0): 269 print("This number is a complete number equal to:",i) 270 i += 1 271 #eg16:A ball dropped from a height of 100 m free. Each fall to the ground after jump back to the original height of half; to fall for it on the 10th floor, the total number of meters after? How high is the tenth bounce? 272 Heigth = int(input("Please enter a Number for Heigth:")) 273 sum = Heigth 274 Times = int(input("Please enter a Number for calculate times:")) 275 print("The Heigth of 10th bounce is equal to :",math.pow(0.5,Times)*Heigth) 276 for i in range(1,Times): 277 sum += 2*Heigth*math.pow(0.5,i) 278 print("The total length is equal to:",sum) 279 #eg17:Two table tennis team for the game, each out of the three. A team for the A, B, C three, B team for the X, y, Z three. Draw a draw. Someone asked the team about the competition. A said he did not x than, C said he does not and Z, X than, please make up the program to find out the list of three teams race 280 for i in range(ord(‘x‘),ord(‘z‘) + 1): 281 for j in range(ord(‘x‘),ord(‘z‘) + 1): 282 if i != j: 283 for k in range(ord(‘x‘),ord(‘z‘) + 1): 284 if (i != k) and (j != k): 285 if (i != ord(‘x‘)) and (k != ord(‘x‘)) and (k != ord(‘z‘)): 286 print(‘order is a -- %s\t b -- %s\tc--%s‘ % (chr(i),chr(j),chr(k))) 287 #这种题目有很强的抽象性,一定要注意求解的方法 288 #eg8:Print out the following pattern (diamond) 289 print(" *") 290 print(" ***") 291 print(" *****") 292 print("*******") 293 print(" *****") 294 print(" ***") 295 print(" *") 296 #eg19:There is a sequence of points: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13, and the sum of the first 20 items of this series. 297 #分析题目的基本规律: 298 #分子:2+3=5 3+5=8 5+8=13 8+13=21 299 #分母:1+2=3 2+3=5 3+5=8 5+8=13 300 Top_num1=2 301 Bot_num1=1 302 Top_num2=3 303 Bot_num2=2 304 sum=7/2 305 for i in range(18): 306 #Num = (Top_num1+Top_num2)/(Bot_num1+Bot_num2) 307 print("The number is :",Top_num1,Bot_num1) 308 Temp1=Top_num1#中间变量的申请和数据更新及存放注意啊!! 309 Temp2=Bot_num1 310 Top_num1 = Top_num2 311 Bot_num1 = Bot_num2 312 Top_num2 = (Temp1+Top_num2) 313 Bot_num2 = (Temp2+Bot_num2) 314 sum += Top_num2/Bot_num2 315 print(sum) 316 #eg20:Use recursive method for 5! 317 def recursive(N): 318 result = N 319 if (N==1): 320 return 1 321 else : 322 result *= recursive(N-1) 323 return result 324 result = recursive(5) 325 print(result) 326 #eg21:By using the recursive function call, the input of the 5 characters, in order to print out the opposite order 327 #Solution 1: 328 FIFO=[‘A‘,‘B‘,‘C‘,‘D‘,‘E‘] 329 print("Please enter five letter for FIFO!") 330 for i in range(5): 331 FIFO[i] = input("Please enter the letter:") 332 print(FIFO[0],FIFO[1],FIFO[2],FIFO[3],FIFO[4]) 333 print(FIFO[4],FIFO[3],FIFO[2],FIFO[1],FIFO[0]) 334 print(FIFO[4]+FIFO[3]+FIFO[2]+FIFO[1]+FIFO[0]) 335 #Solution 2(采用递归算法): 336 def output(s,l): 337 if l==0: 338 return 339 print(s[l-1]) 340 output(s,l-1) 341 s = FIFO[4]+FIFO[3]+FIFO[2]+FIFO[1]+FIFO[0] 342 l = len(s) 343 output(s,l) 344 #eg22:A 5 digit number, it is not a palindrome judgment. That 12321 is a palindrome, a bit and bit the same ten million, with thousands of the same 345 346 #del Num 347 Num = 12321 348 print("The number for test is :",Num) 349 if (int(Num/10000)==Num%10) and (int(Num/1000)%10==int((Num%100)/10)): 350 print("This is a palindrome number!") 351 #eg23:Please enter the first letter of the week to determine what is a week, if the first letter, then continue to determine the second letters 352 days = [‘Monday‘,‘Thuesday‘,‘Wednesday‘,‘Thursday‘,‘Friday‘,‘Saturday‘,‘Sunday‘] 353 print("Please enter the first letter to determine the day in week:") 354 Ch1=input("Please enter the first letter:") 355 if Ch1==‘M‘: 356 print("Today is Monday!") 357 elif Ch1==‘W‘: 358 print("Today is Wednsday!") 359 elif Ch1==‘F‘: 360 print("Today is Friday!") 361 elif Ch1==‘T‘: 362 print("Please enter the second letter for determined!") 363 Ch2=input() 364 if Ch2==‘h‘: 365 print("Today is Thuesday!") 366 else : 367 print("Today is Tursday!") 368 else: 369 print("Please enter the second letter for detemined!") 370 Ch2=input() 371 if Ch2==‘a‘: 372 print("Today is Saturday!") 373 else : 374 print("Today is Sunday!") 375 #eg24:Comma separated list按逗号分隔列表 376 L = [1,2,3,4,5] 377 s1 = ‘,‘.join(str(n) for n in L) 378 print(s1) 379 #eg25:文本颜色设置--示例35 380 class bcolors: 381 HEADER = ‘\033[95m‘ 382 OKBLUE = ‘\033[94m‘ 383 OKGREEN = ‘\033[92m‘ 384 WARNING = ‘\033[93m‘ 385 FAIL = ‘\033[91m‘ 386 ENDC = ‘\033[0m‘ 387 BOLD = ‘\033[1m‘ 388 UNDERLINE = ‘\033[4m‘ 389 print(bcolors.WARNING + "Warning mesg color?" + bcolors.ENDC)
标签:
原文地址:http://www.cnblogs.com/uestc-mm/p/5719681.html