During the winter vacation, in addition to doing some other courses, I was also reviewing the basic knowledge of Python. In the summer vacation of 19, I studied Python for more than a month, but I didn't have much time and energy to learn it well. During the winter vacation, I used my own ideas to compile the after-school exercises of the national computer level examination, python II, with my own ideas. If I didn't have any accidents, I I will take the python Level 2 national level examination in 2020. I hope it's going well.
The following one to five chapters of the code to you paste out, welcome to refer to and criticism.
Chapter 1
#1 output corresponding results according to user input
name = input("Please enter the name of the other party:") str= input("Please input the content of the whisper:") print("{} Please listen to my whisper:{}".format(name,str))
#2 nine times nine multiplication table output. Nine nine multiplication tables commonly used in neat printing
for i in range(1,10): for j in range(1, i+1): print(" {}*{} = {:2} ".format(j,i,i*j),end = '') print('') # I don't understand why we have to add this here as a line feed operation for each line. If we don't add this, we will squeeze into one block, and we can also use ('\ t')
#3 drawing of tangent circle
import turtle turtle.pensize(3) turtle.circle(20) # Draw a circle with a radius of 10 pixels turtle.circle(40) # Draw a circle with a radius of 20 pixels turtle.circle(80) # Draw a circle with a radius of 40 pixels turtle.circle(160) # Draw a circle with a radius of 80 pixels
#4. The system prompts to input three hobbies and output them together
hobbies = '' for i in range(3): h = input("Please input your hobbies (up to three, press Q perhaps q End):") if h.upper() == 'Q': break hobbies += h + ' ' print("Your hobbies are:{}".format(hobbies))
hobbies = '' for i in range(3): h = input("Please input your hobbies (up to three, press Q perhaps q End):") if h.upper() == 'Q': break hobbies += h + ' ' print("Your hobbies are:{}".format(hobbies))
The second chapter
#1 obtain an integer N input by the user, calculate and output the 32 power of N
N = int(input("Please enter an integer:")) # Or N = eval(input("please enter an integer:") n = 32 sum = 1 while n > 0: sum = sum * N n = n - 1 print(sum) # Or in a more speculative way, we can get the answer directly print(N**32)
#2 get a piece of text entered by the user and output it vertically
list = input("Please enter a text:") for i in list: print(i)
#3. Obtain a legal calculation formula of user input, for example: 1.2 + 3.4, output the calculation result
N = eval(input("Please enter a legal formula, for example: 1.2+3.4,The operation result is:")) # The key is to get rid of that quotation mark print(N)
#4 get a decimal of user input, extract and output its integer part
N = input("Please enter a decimal:") # You can't use eval(input()) or float(input()) directly for this problem, because the numbers you get are not iterative. Remember for i in N: if i != '.': print(i,end = '') else: break # Or you can use python's built-in function round() method to directly put it in place (print(round(N)))
#5 the following code can obtain an integer N input by the user, calculate and output the sum of 1 to N. Then there are several syntax errors in this code. Please point out the errors and correct them
n = int(input("please enter an integer N: ")) sum = 0 for i in range(1,n+1): # The value of n must be increased by one first. You can create a test file to test, which is convenient for observation sum += i print("1 reach N Sum result:{}".format(sum))
The third chapter
#1. Get an integer input by the user, and output the integer with hundreds of flavors and above
int_number = int(input("Please enter an integer:")) num = int_number // 100 print(round(num))
#2 obtain a string entered by the user, separate the strings by spaces, and print them out line by line
L_char = input("Please enter a string:") L_char = L_char.split(' ') for i in L_char: print(i)
#The program reads in a number (1-7) representing the day of the week and outputs the corresponding week string name. For example, enter 3 to return to Wednesday.
N = eval(input("Please enter a number (1-7):")) week_list = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] print(week_list[N - 1])
#4 let n be an arbitrary natural number. If the natural number obtained from the reverse arrangement of the digits of n is equal to N, then n is called palindrome number. Input a 5-digit number from the keyboard, please write a program to determine whether this number is palindrome number?
n = input("Please enter a number:") L = len(n) s = 0 for i in range(L): if n[i] == n[-i-1]: s = s + 1 if s == L: print("Palindrome number") else: continue else: print("Not palindrome") break # There is a simpler way to write this question. Please try it yourself
#5 input a decimal integer and output its binary, octal and hexadecimal strings respectively
N = eval(input("Please enter a decimal integer:")) print("{1} The binary of is:{0:b} ,Octal is:{0:o},Hex is:{0:x}".format(N,N)) # It should be noted that the given parameters are not consistent with the actual parameters. How to solve the problem
The fourth chapter
#1 enter whether the output of a year is a leap year. The condition of leap year is that the year that can be divided by 4 but cannot be divided by 100, or the year that can be divided by 400 is leap year.
try: year = eval(input("Please enter a year:")) if type(year) == type(100): # Here to determine whether the user input is an integer, 100 can also be other integers if year % 4 == 0: if year % 400 == 0 or year % 100 != 0: print("This number {} It is a leap year.".format(year)) else: print("This number {} Not a leap year.".format(year)) else: print("This number {} Not a leap year.".format(year)) except: print("You have not entered an integer, please re-enter!")
#2 Calculation of maximum common divisor
num1 = eval(input("Please enter the first number:")) num2 = eval(input("Please enter the second number:")) if num2 >= num1: # Make sure the maximum number is in front num1,num2 = num2,num1 else: pass while num1 % num2 : # Rolling phase division temp = num1 % num2 num1 = num2 num2 = temp # The final num2 is the greatest common divisor print("The greatest common divisor is {} ".format(num2))
#3 Calculation of minimum common multiple
num1 = eval(input("Please enter the first number:")) num2 = eval(input("Please enter the second number:")) def lcm(num1,num2): last_small = max(num1,num2) while True: if (last_small % num1) == 0 and (last_small % num2) == 0: return last_small break last_small += 1 print("The minimum common multiple is {} ".format(lcm(num1,num2)))
#4. Count the number of different characters.
''' //Users input a line of characters from the keyboard, write a program, count and output the number of English characters, numbers, spaces and other characters ''' L_char = input("Please enter a string of characters:") counter1,counter2,counter3,counter4 = 0,0,0,0 for c in L_char: if ('a' <= c <= 'z') or ('A' <= c <= 'Z') : counter1 += 1 elif ('0' <= c <= '9'): counter2 += 1 elif (c == ' '): counter3 += 1 else: counter4 += 1 print("Chinese and English characters {} Digits {} Spaces and spaces {} individual,Other characters {} individual".format(counter1,counter2,counter3,counter4))
#5 guessing game continued.
''' //When the user input is not an integer (such as letters, floating-point numbers), the program will terminate execution and exit. The procedure for adapting Title 1, //When the user makes an error in input, he will be prompted that the input must be an integer, and ask the user to input again ''' import random target = random.randint(1,1000) count = 0 while True: try: guess = eval(input('Please enter a guess integer (1-1000): ')) count += 1 if guess > target: print("Guess big!") elif guess < target: print("Guess it's small!") else: print("Guess right!") break except: print("Input must be an integer") continue print("The number of guesses in this round is:",count)
#6 sheep door problem
''' //There are three closed doors. Behind one is the car. Behind the other is the goat. Only the host knows what is behind each door. Contestants can choose a door, //Before opening it, the host will open another door, expose the goat after going out, and then allow participants to change their options. //Can you increase your chance to guess the right car after the competitors change their options? ——This is a classic question. Please use random library for this random event //Forecast, output the probability that the contestants change their choice and insist on winning. ''' import random reference = 10000 # As a reference, the larger the base selection, the closer it is to the actual situation, similar to the ancient coin tossing experiment guess_right_pro = 0 guess_change_pro = 0 for i in range(1,1+reference): guess = random.randint(1, 3) real = random.randint(1, 3) if guess == real: guess_right_pro += 1 else: guess_change_pro += 1 print("The probability of getting a car without changing the choice is:{} ".format(guess_right_pro / reference)) print("The probability that you can change your choice to get a car is:{} ".format(guess_change_pro / reference)) if guess_right_pro > guess_change_pro: print("After the contestants change their choice, they can increase the chance to guess the car!") else: print("After the contestants change their choice, they can't increase the chance to guess the car!")
The fifth chapter
#1 implements the isNum() function. The parameter is a string. If the string is an integer, a floating-point number, or a complex number, it returns True. Otherwise, it returns False
def isNum(string): if isinstance(eval(string),int) or isinstance(eval(string),float) or isinstance(eval(string),complex): return True else: return False try: string = input("Please enter a string:") print(isNum(string)) except: print("What you entered is not a legal string, please re-enter!")
#2. Implement isPrime() function. The parameter is an integer. Exception handling is required. Returns True if the integer is prime, False otherwise
def isPrime(num): for i in range(2,num): #Judge whether it is a prime number if num % i == 0: return False return True while True: # Judgment of abnormal circulation try: num = eval(input("Please enter an integer:")) print(isPrime(num)) break except: print("What you input is not an integer!") continue
#3 write a function to calculate the number of numbers, letters, spaces and other characters in the incoming string
def string_num(L_char): counter1,counter2,counter3,counter4 = 0,0,0,0 for c in L_char: if ('a' <= c <= 'z') or ('A' <= c <= 'Z') : counter1 += 1 elif ('0' <= c <= '9'): counter2 += 1 elif (c == ' '): counter3 += 1 else: counter4 += 1 print("Chinese and English characters {} Digits {} Spaces and spaces {} individual,Other characters {} individual".format(counter1,counter2,counter3,counter4)) L_char = input("Please enter a string of characters:") string_num(L_char)
#4 write a function to print all prime numbers within 200, separated by spaces
def print_prime_200(): for i in range(1,201): for j in range(2,i): if i % j == 0: break else: # print(i,end = ') is not allowed in this place, otherwise all numbers will be printed directly print(i,end=' ') # The reason for using else statement is that else statement can only be executed after the previous for() statement is executed. Pay attention to the precedence print_prime_200()
#5 write a function with an integer n as the parameter. Use recursion to get the nth number in fibolacci sequence, and return.
def feibo(n): if n <= 2: return 1 else: return feibo(n-1) + feibo(n-2) # Add one calculation by recursion n = eval(input("Please enter an integer n:")) print(feibo(n))