题目要求:

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)

用到的知识点:

  1. try/except 抛出异常后,用continue 结束本次循环进入下次循环,阻止循环终止。
  2. while 是 indefinite iteration; for 是definite iteration。区别在于:for 在循环体之前就知道循环的次数。
  3. python 的数据格式:Int, Float, String, Boolean,None
  4. break: Exits the currently executing loop
  5. continue: Jumps to the "top" of the loop and starts the next iteration
  6. 'is' and 'is not' Operators: 
    1. both use in logic expressions
    2. 'is': similar to, but stronger than ==, implies 'is the same as'
    3. you shouldn’t use 'is' when you should use '=='
    4. 'is' used for 'True', 'False', 'None'.
    5. don’t use 'is' frequently, cause it’s strong equality, stronger than ==

Python: find the smallest and largest value的更多相关文章

  1. [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 ...

  2. 【python cookbook】【数据结构与算法】4.找到最大或最小的N个元素

    问题:想在某个集合中找出最大或最小的N个元素 解决方案:heapq模块中的nlargest()和nsmallest()两个函数正是我们需要的. >>> import heapq &g ...

  3. Wing IDE 5 for Python 安装及破解方法

    安装Wing IDE 官网下载deb安装文件 开始安装程序 dpkg -i 文件名.deb 安装完成后打开注册界面,输入下面的License ID 后得到RequestCode,将RequestCod ...

  4. python模块:random

    """Random variable generators. integers -------- uniform within range sequences ----- ...

  5. python tricks

    1. cities = ['Marseille', 'Amsterdam', 'New York', 'Londom'] # the good way for i, city in enumerate ...

  6. kafka实战教程(python操作kafka),kafka配置文件详解

    kafka实战教程(python操作kafka),kafka配置文件详解 应用往Kafka写数据的原因有很多:用户行为分析.日志存储.异步通信等.多样化的使用场景带来了多样化的需求:消息是否能丢失?是 ...

  7. python操作kafka

    python操作kafka 一.什么是kafka kafka特性: (1) 通过磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能. (2) 高吞吐量 :即使是 ...

  8. 一些实验中用过的python函数/方法(持续更新)

    衡量运行时间 很多时候你需要计算某段代码执行所需的时间,可以使用 time 模块来实现这个功能. import time startTime = time.time() # write your co ...

  9. python ide ---wing 注册机

    注册机脚本代码如下: import sha import string BASE2 = '01' BASE10 = '0123456789' BASE16 = '0123456789ABCDEF' B ...

随机推荐

  1. pcb走线注意事项笔记

    一.高压隔离. PCB的安全距离: 1.电气间隙或者叫做控件距离. (两相邻的后者一个到相邻电机壳表面的沿空气测量的最短距离,电气间隙的决定,根据测量的工作电压以及绝缘等级就可以决定距离.) a.一次 ...

  2. JetBrains WebStorm打开多个项目project的方法

    JetBrains WebStorm打开多个项目project的方法File-->Settings-->Directories点击右侧 + Add content root,选择目录后即可 ...

  3. Spring SpringBoot和SpringCloud的关系

    Spring SpringBoot和SpringCloud的关系 Spring Cloud 是完全基于 Spring Boot 而开发,Spring Cloud 利用 Spring Boot 特性整合 ...

  4. 关于js闭包之小问题大错误

    闭包是 JavaScript 开发的一个关键方面:匿名函数可以访问父级作用域的变量. 如果闭包的作用域中保存着一个 HTML 元素,则该元素无法被销毁.(下面代码来自高程) 刚看到一个关于闭包自己没注 ...

  5. linux 二级域名设置

    首先,你的拥有一个有泛域名解析的顶级域名,例如: domain.com 其次,在 httpd.conf 中打开 mod_rewrite 之后,在 httpd.conf 的最后,添加以下内容: Rewr ...

  6. [转载]转,Oracle中关于处理小数点位数的几个函数,取小数位数,Oracle查询函数

    关于处理小数点位数的几个oracle函数() 1. 取四舍五入的几位小数 select round(1.2345, 3) from dual; 结果:1.235 2. 保留两位小数,只舍 select ...

  7. AURO OtoSys IM100 vs Lonsdor K518ISE: which better?

    Comparison: AURO OtoSys IM100 and Lonsdor K518ISE It’s aimed to help make a purchase of decent auto ...

  8. IntelliJ IDEA. Debug模式

    资料收集: https://www.bilibili.com/video/av6749471/?p=16 eclipse debug模式. 基础 Intellij Idea--Debug使用 Inte ...

  9. mysql备份与恢复-xtracebackup

    因为percona打算放弃使用innobackupex备份工具,因此我们这里也说明一下innobackupex的兄弟工具xtraceback工具的使用 这个工具的安装可以参考上面的一些博文,上面详细说 ...

  10. python简说(二十三)发邮件

    import yagmailusername='uitestp4p@163.com'password='houyafan123'#生成授权码,qq.163.126都是授权码 mail_server = ...