1. Basic questions
- Print pass or fail according to the range of grades entered
score = int(input('Enter grade:')) if score >= 60: print('pass') else: print('fail,')
- Print adults or minors according to the entered age range. If the age is not within the normal range (0 ~ 150), it is not a person!.
age = int(input('Enter age:')) if 0 <= age <= 150: if age >= 18: print('adult') else: print('under age') else: print('This is not a person!')
- Enter two integers a and b. if the result of a-b is odd, the result will be output. Otherwise, the result of output prompt information a-b is not odd.
a = int(input('input a: ')) b = int(input('input b: ')) if (a-b) % 2 == 0: print('a-b The result is not odd') else: print(a-b)
- Enter the year. If the entered year is a leap year, print 'leap year', otherwise print 'normal year'
year = int(input('Enter year:')) result_1 = (year % 4 == 0 and year % 10 != 0) or year % 400 == 0 if result_1: print('leap year') else: print('Ordinary year')
- Use the for loop to output multiples of all 3 in 0 ~ 100.
for i in range(0, 101, 3): print(i)
- Use the for loop to output the number of single digits or tens within 100 ~ 200 that can be divided by 3.
for i in range(100, 200): num = i // 10 % 10 num_1 = i % 10 if num % 3 == 0 or num_1 % 3 == 0: print(i)
- Count the number of tens in 100 ~ 200 that are 5
for i in range(100, 200): num = i % 10 if num % 10 == 5: print(i)
- Print all numbers in 50 ~ 150 that can be divided by 3 but cannot be divided by 5
for i in range(50, 150): if i % 3 == 0 and i % 5 != 0: print(i)
- Calculate the sum of all numbers in 50 ~ 150 that can be divided by 3 but cannot be divided by 5
sum_1 = 0 for i in range(50, 150): if i % 3 == 0 and i % 5 != 0: sum_1 += i print(sum_1)
2. Advanced questions
- Use the cycle to calculate the result of 1 * 2 * 3 * 4 *... * 10.
sum_1 = 1 for i in range(1, 11): sum_1 *= i print(sum_1)
- Count the number of digits within 100 that are 2 and can be divided by 3.
count = 0 for i in range(1, 101): if i % 3 == 0 and i % 10 == 2: count += 1 print(count)
- Enter any positive integer and find how many digits it is?
Note: you can't use strings here. You can only use loops
num = int(input('Enter a positive integer:')) count = 0 while num // 10 != 0: count += 1 num = num // 10 else: count += 1 print(count)
-
Print out all the numbers of daffodils. The so-called daffodils number refers to a three digit number, and the square sum of each digit is equal to the number itself. For example: 153 yes
The number of fairy flowers is 1 ³ + five ³ + three ³ Equals 153.
for i in range(100, 1000): x = i % 10 y = i % 100 // 10 z = i % 1000 // 100 if x**3 + y**3 + z**3 == i: print(i)
3. Challenges
- Judge whether the specified number is a prime number (a prime number is a prime number, that is, a number that cannot be divided by other numbers except 1 and itself)
num = int(input('Please enter a number:')) if num % 2 != 0 and num % 3 != 0: print('It's a prime') else: print('Not prime')
- Find the value of the nth number in the Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34... (here n can be any positive integer, which can be determined by input)
n = int(input('Enter number n: ')) if n <= 2: print(1) else: num_1 = 1 num_2 = 1 for i in range(n - 2): num = num_1 + num_2 num_2 = num_1 num_1 = num print(num)
- Output 9 * 9 formula. Procedure analysis: Considering branches and columns, there are 9 rows and 9 columns in total, i control row and j control column.
for i in range(1, 10): for j in range(1, 10): print(i, '*', j, '=', i*j, end=' ', sep='') print('')
- This is the classic question of "100 horses and 100 loads". There are 100 horses, carrying 100 loads of goods, 3 loads of horses, 2 loads of medium horses and 1 load of two ponies. How many are the big, medium and small horses? (exhaustive method can be used directly)
for a in range(34): for b in range(100 - a): if a * 3 + b * 2 + (100 - a - b) / 2 == 100: print('Malaysia has', a, 'Horse', 'Zhongma you', b, 'Horse', 'Pony has', 100-a-b, 'Horse', sep='')