题目要求:

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. restful的特点

    1. 资源(Resources) REST的名称”表现层状态转化”中,省略了主语.”表现层”其实指的是”资源”(Resources)的”表现层”.                所谓”资源”,就是网络 ...

  2. php 当前时间 当前时间戳和数据库里取出的时间datetime格式进行比较大小

    php 当前时间 当前时间戳和数据库里取出的时间datetime格式进行比较大小 UNIX时间戳转换为日期用函数: date() ,date('Y-m-d H:i:s', 1500219870); 日 ...

  3. Django 应用 静态文件配置

    Django 应用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. 算法提高 11-1实现strcmp函数

    问题描述 自己实现一个比较字符串大小的函数,也即实现strcmp函数.函数:int myStrcmp(char *s1,char *s2) 按照ASCII顺序比较字符串s1与s2.若s1与s2相等返回 ...

  5. urlopen和urlretrieve

    import urllib import re url = "https://www.duitang.com/search/?kw=%E9%AC%BC%E6%80%AA&type=f ...

  6. 【题解】Luogu P2257 YY的GCD

    原题传送门 这题需要运用莫比乌斯反演(懵逼钨丝繁衍) 显然题目的答案就是\[ Ans=\sum_{i=1}^N\sum_{j=1}^M[gcd(i,j)=prime]\] 我们先设设F(n)表示满足\ ...

  7. indexOf()/equals/contains

    indexOf():对大小写敏感定义:返回某个指定字符串值在字符串中首次出现位置用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1:方便判断 ...

  8. 解决*.props打开失败问题

    由于不同机器的绝对地址不一样,可能会出现解决*.props打开失败问题,解决方向如下: 1.找到这里缺失的.props文件,复制到固定路径下: 2.强行打开代码,这个时候是报错的 3.选择编辑 4.将 ...

  9. FBX SDK在vs 2010下面的配置

    1.下载FBS SDK.地址.因为我是vs2010,所以我下载的是FBX SDK 2016.1.2 VS2010.如果没有了,你可以找博主直接要,QQ1240957820. 2.下载下来的是一个exe ...

  10. java后端学习路线

    java基础-->java设计模式-->java数据结构与算法