'''Program 31] Question: Enter the first letter of the day of the week to determine the day of the week. If the first letter is the same, continue to judge the second letter. 1. Program Analysis: it is better to use the case statement. if the first letter is the same, the case statement or if statement is used to determine the second letter. 2. program source code: ''' from sys import stdinletter = stdin. read (1) stdin. flush () while letter! = 'Y': if letter = 's': print 'Please input second letter 'letter = stdin. read (1) stdin. flush () if letter = 'A': print 'saturday' elif letter = 'U': print 'sunday' else: print 'data error' break elif letter = 'F': print 'Friday' break elif letter = 'M ': print 'monday' # break elif letter = 'T': print 'Please input second letter 'ter = stdin. read (1) stdin. flush () if letter = 'U': print 'tuesday' elif letter = 'H': print 'thursday' else: print 'data error' break elif letter = 'W': print 'wednesday' else: print 'data error' letter = stdin. read (1) stdin. flush ()
''' [Program 36] Question: Calculate the prime number within 100 1. program Analysis: 2. program source code: ''' from math import sqrtif _ name _ = '_ main _': N = 100 a = range (0, N) for I in range (2, int (sqrt (N): for j in range (I + 1, N): if (a [I]! = 0) and (a [j]! = 0): if a [j] % a [I] = 0: a [j] = 0 print for I in range (2, N ): if a [I]! = 0: print "% 5d" % a [I] if (I-2) % 10 = 0: print
''' [Program 37] Question: Sorting 10 numbers 1. program Analysis: You can use the selection method, that is, from the last nine comparison processes, select a minimum to exchange with the first element, and so on, that is, compare the second element with the last eight, and exchange. 2. program source code: ''' if _ name _ = "_ main _": N = 10 # input data print 'Please input ten num: \ n'l = [] for I in range (n): l. append (int (raw_input ('input a number: \ n') print for I in range (n ): print l [I] print # sort ten num for I in range (N-1): min = I for j in range (I + 1, N ): if l [min]> l [j]: min = j l [I], l [min] = l [min], l [I] print 'after sorted' for I in range (N): print l [I]
''' [Program 38] Question: Calculate the sum of the diagonal elements of a 3*3 matrix. 1. program analysis: the dual for loop is used to control the input two-dimensional array, and then a [I] [I] is accumulated and output. 2. source code of the program: ''' if _ name _ = '_ main _': a = [] sum = 0.0 for I in range (3):. append ([]) for j in range (3): a [I]. append (float (raw_input ("input num: \ n") for I in range (3): sum + = a [I] [I] print sum
''' [Program 39] Question: There is an array sorted. Enter a number to insert it into the array according to the original rule. 1. Program Analysis: first, determine whether the number is greater than the last number, and then consider the number inserted in the middle. After the element is inserted, the number is shifted to the next position. 2. program source code: ''' if _ name _ = '_ main _': # method 1 a = [40,100, 0] print 'original list is: 'for I in range (len (a): print a [I] number = int (raw_input ("insert a new number: \ n ") end = a [9] if number> end: a [10] = number else: for I in range (10): if a [I]> number: temp1 = a [I] a [I] = number for j in range (I + 1, 11 ): temp2 = a [j] a [j] = temp1 temp1 = temp2 break for I in range (11 ): print a [I] # method 2 # insrt another number = int (raw_input ('input a number: \ n') if number> a [len () -1]:. append (number) else: for I in range (len (a): if a [I]> number:. insert (I, number) print
''' [Program 40]: returns an array in reverse order. 1. Program Analysis: Use the first and last exchanges. 2. program source code: ''' if _ name _ = '_ main _': a = [9, 6, 5, 4, 1] N = len () print a for I in range (len (a)/2): a [I], a [N-I-1] = a [N-I-1], a [I] print