当前位置:网站首页>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
passperhapsfail,.achievement = int(input(' Please enter the grade :'))Print according to the entered age range
adultperhapsA 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 numbera = 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 ')
边栏推荐
- 1.17 learning summary
- Transport layer TCP protocol and UDP protocol
- 微服务之间的Token传递之一@Feign的token传递
- YOLOv5-6.0的一些参数设置和特征图可视化
- Problem follow up - PIP source change
- 天才制造者:獨行俠、科技巨頭和AI|深度學習崛起十年
- Use fill and fill in Matplotlib_ Between fill the blank area between functions
- Astype conversion data type
- Image translation /gan:unsupervised image-to-image translation with self attention networks
- Nabicat连接:本地Mysql&&云服务Mysql以及报错
猜你喜欢

Zuul 實現動態路由

Illustration of ONEFLOW's learning rate adjustment strategy

Using requests library and re library to crawl web pages

Image translation /gan:unsupervised image-to-image translation with self attention networks

Machine learning final exercises

0622 horse palm fell 9%

DBeaver 安装及配置离线驱动

torchvision_transform(图像增强)

Multipass中文文档-远程使用Multipass

5. <tag-栈和常规问题>补充: lt.946. 验证栈序列(同剑指 Offer 31. 栈的压入、弹出序列)
随机推荐
Multipass中文文档-使用实例命令别名
Statsmodels Library -- linear regression model
Use to_ Numeric to numeric type
Is education important or ability important in software testing
A ZABBIX self discovery script (shell Basics)
Zuul 實現動態路由
LeetCode 94. Middle order traversal of binary tree
Anti withdrawal test record
文件上传与安全狗
2022.2.13
Illustration of ONEFLOW's learning rate adjustment strategy
[latex] error type summary (hold the change)
ThreadPoolExecutor implements file uploading and batch inserting data
Multipass中文文档-移除实例
Large numbers (C language)
File upload and security dog
Créateur de génie: cavalier solitaire, magnat de la technologie et ai | dix ans d'apprentissage profond
2.< tag-动态规划和常规问题>lt.343. 整数拆分
微信小程序保存圖片的方法
Zuul 实现动态路由