当前位置:网站首页>Day4 branch and loop jobs
Day4 branch and loop jobs
2022-06-26 04:59:00 【The story of Ula】
Basic questions
Print according to the range of grades entered
pass
perhapsfail,
.achievement = int(input(' Please enter the grade :'))
Print according to the entered age range
adult
perhapsA minor
, If the age is not within the normal range (0~150) PrintThis is not a person !
.age = int(input(' Please enter age :')) if age > 150 or age < 0: print(' This is not a person ') elif age >= 18: print(' adult ') else: print(' A minor ')
Enter two integers a and b, if a-b The result is an odd number , The result is output , Otherwise, the prompt message will be output
a-b The result is not an odd number
a = int(input(' Please enter an integer :')) b = int(input(' Please enter an integer :')) if (a - b) % 2 == 0: print('a - b The result is not an odd number ') else: print(a - b)
Use for Cyclic output 0~100 All in 3 Multiple .
for i in range(0, 101, 3): print(i)
Use for Cyclic output 100~200 The inner single digit or ten digit can be 3 Divisible number .
for i in range(100, 201): if i // 10 % 10 % 3 == 0 or i % 10 % 3 == 0: print(i)
Use for Cycle statistics 100~200 The median ten is 5 The number of
for i in range(100, 200): if i // 10 % 10 == 5: print(i)
Use for Loop printing 50~150 All can be 3 Divisible but not by 5 Divisible number
for i in range(51, 151, 3): if i % 5 != 0: print(i)
Use for Cycle calculation 50~150 All can be 3 Divisible but not by 5 The sum of divisible numbers
count = 0 for i in range(51, 151, 3): if i % 5 != 0: count += i print(count)
Statistics 100 The inner single digits are 2 And can be 3 The number of integers .
for i in range(12, 101, 3): if i % 10 == 2: print(i)
Advanced questions
Enter any positive integer , Ask him how many digits ?
Be careful : You can't use strings here , Only loop
num = int(input(' Please enter a positive integer :')) for i in range(0, num): if num // 10 ** i == 0: break print(i)
Print out all the daffodils , The so-called narcissus number refers to a three digit number , Its figures ⽴ The sum of the squares is equal to the number itself .例 Such as :153 yes
⼀ individual ⽔ Fairy flower number , because
1³ + 5³ + 3³
be equal to 153.for i in range(100, 1000): bw = i // 100 sw = i // 10 % 10 gw = i % 10 if bw ** 3 + sw ** 3 + gw ** 3 == i: print(i)
Judge whether the specified number is a prime number ( Prime numbers are prime numbers , In addition to 1 A number other than itself that cannot be divided by other numbers )
num = int(input(' Please enter one digit :')) for i in range(2, num): if num % i == 0: print(f'{ num} Not primes ') break else: print(f'{ num} Prime number ')
Output 9*9 formula . Program analysis : Branch and column considerations , common 9 That's ok 9 Column ,i The control line ,j Control the column .
for i in range(1, 10): for j in range(1, i + 1): print(f'{ i}* { j}= { i * j} ', end=' ') print()
This is the classic " A hundred horses and a hundred burdens " problem , There are a hundred horses , Carry a hundred loads , Big horse, big horse 3 Dan , On the back of a horse 2 Dan , Two ponies carry 1 Dan , How big is it , in , How many ponies each ?( You can directly use the exhaustive method )
for i in range(34): for j in range(51): if i * 3 + j * 2 + (100 - i - j) / 2 == 100: print(f' Malaysia { i} A medium horse { j} A pony { 100- i - j } horse ')
边栏推荐
- Dbeaver installation and configuration of offline driver
- Codeforces Round #800 (Div. 2)
- Final review of brain and cognitive science
- LeetCode 19. 删除链表的倒数第 N 个结点
- 2022.2.11
- 2022.1.24
- UWB ultra high precision positioning system architecture
- 文件上传与安全狗
- Machine learning final exercises
- Nabicat connection: local MySQL & cloud service MySQL and error reporting
猜你喜欢
5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)
超高精度定位系统中的UWB是什么
Zuul 实现动态路由
6.1 - 6.2 Introduction à la cryptographie à clé publique
Codeforces Round #800 (Div. 2)
0622 horse palm fell 9%
Zuul implements dynamic routing
torchvision_transform(图像增强)
PSIM software learning ---08 call of C program block
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
随机推荐
Floyd
#微信小程序# 在小程序里面退出退出小程序(navigator以及API--wx.exitMiniProgram)
Problem follow up - PIP source change
ROS notes (07) - Implementation of client and server
2022.1.24
Dameng database backup and restore
YOLOV5超参数设置与数据增强解析
Pycharm package import error without warning
Multipass Chinese document - share data with instances
[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions
2.< tag-动态规划和常规问题>lt.343. 整数拆分
微信小程序保存图片的方法
YOLOV5训练结果的解释
MySql如何删除所有多余的重复数据
numpy 通用函数
How to use the configured slave data source for the scheduled task configuration class scheduleconfig
ModuleNotFoundError: No module named ‘numpy‘
做软件测试学历重要还是能力重要
6.1 - 6.2 公钥密码学简介
Record a circular reference problem