Python: find the smallest and largest value
题目要求:
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. [ Programming for Everybody (Getting Started with Python) 5.2 assignment ]
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" : break
try:
value = int(num)
except:
print("Invalid input")
continue
if largest is None or smallest is None:
largest = value
smallest = value
elif smallest > value:
smallest = value
elif largest < value:
largest = value
print("Maximum is", largest)
print("Minimum is", smallest)
用到的知识点:
- try/except 抛出异常后,用continue 结束本次循环进入下次循环,阻止循环终止。
- while 是 indefinite iteration; for 是definite iteration。区别在于:for 在循环体之前就知道循环的次数。
- python 的数据格式:Int, Float, String, Boolean,None
- break: Exits the currently executing loop
- continue: Jumps to the "top" of the loop and starts the next iteration
- 'is' and 'is not' Operators:
- both use in logic expressions
- 'is': similar to, but stronger than ==, implies 'is the same as'
- you shouldn’t use 'is' when you should use '=='
- 'is' used for 'True', 'False', 'None'.
- don’t use 'is' frequently, cause it’s strong equality, stronger than ==
Python: find the smallest and largest value的更多相关文章
- [LeetCode&Python] Problem 908. Smallest Range I
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...
- 【python cookbook】【数据结构与算法】4.找到最大或最小的N个元素
问题:想在某个集合中找出最大或最小的N个元素 解决方案:heapq模块中的nlargest()和nsmallest()两个函数正是我们需要的. >>> import heapq &g ...
- Wing IDE 5 for Python 安装及破解方法
安装Wing IDE 官网下载deb安装文件 开始安装程序 dpkg -i 文件名.deb 安装完成后打开注册界面,输入下面的License ID 后得到RequestCode,将RequestCod ...
- python模块:random
"""Random variable generators. integers -------- uniform within range sequences ----- ...
- python tricks
1. cities = ['Marseille', 'Amsterdam', 'New York', 'Londom'] # the good way for i, city in enumerate ...
- kafka实战教程(python操作kafka),kafka配置文件详解
kafka实战教程(python操作kafka),kafka配置文件详解 应用往Kafka写数据的原因有很多:用户行为分析.日志存储.异步通信等.多样化的使用场景带来了多样化的需求:消息是否能丢失?是 ...
- python操作kafka
python操作kafka 一.什么是kafka kafka特性: (1) 通过磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能. (2) 高吞吐量 :即使是 ...
- 一些实验中用过的python函数/方法(持续更新)
衡量运行时间 很多时候你需要计算某段代码执行所需的时间,可以使用 time 模块来实现这个功能. import time startTime = time.time() # write your co ...
- python ide ---wing 注册机
注册机脚本代码如下: import sha import string BASE2 = '01' BASE10 = '0123456789' BASE16 = '0123456789ABCDEF' B ...
随机推荐
- bootstrapValidator验证表单后清除当次验证的方法
用bootstrapValidator的resetForm()方法: <!-- // create server begin --> <div class="modal f ...
- WordConuts
import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import java.uti ...
- Hadoop学习笔记之六:HDFS功能逻辑(2)
Lease(租约) HDFS(及大多数分布式文件系统)不支持文件并发写,Lease是HDFS用于保证唯一写的手段. Lease可以看做是一把带时间限制的写锁,仅持有写锁的客户端可以写文件. 租约的有效 ...
- Java中在java.sql.Date的系统时间上加上30天并写入oracle
在java.sql.Date的系统时间上加上30天,并写入oracle 思路:通过 Calendar.getInstance() 获得对象,然后 add() 方法添加 时间,再通过 new java. ...
- vue editorConfig
在文件目录下, indent_size = 2设置为4
- Linux Centos 7.4 内核升级
Linux Centos 7.4 内核升级 原始内核版本:3.10.0-693.2.2.el7.x86_64 升级内核版本:4.14.9-1.el7.elrepo.x86_64 1.导入key Key ...
- javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 解决方法
在使用hibernate validator进行参数有效性校验的时候,我们有时候会遇到javax.validation.UnexpectedTypeException: HV000030: No va ...
- mysql不常用但很有用的语句整理
mysqld_multi多实例停止.启动 mysqld_multi --defaults-file=/etc/my.cnf start 1,2 mysqld_multi --defaults-file ...
- 寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)
一.问题提出 这是一个来自"answerOpenCV"(http://answers.opencv.org/question/200422/opencv-c-filling-hol ...
- py4CV例子2汽车检测和svm算法
1.什么是汽车检测数据集: ) pos, neg = , ) matcher = cv2.FlannBasedMatcher(flann_params, {}) bow_kmeans_trainer ...