**

Exercises

**
1.
Many programming languages have sign available as a built-in function. Python doesn’t, but we can define our own!

In the cell below, define a function called sign which takes a numerical argument and returns -1 if it’s negative, 1 if it’s positive, and 0 if it’s 0.

# Your code goes here. Define a function called 'sign'
def sign(num):if num > 0:num = 1elif num < 0:num = -1else:num = 0return num
# Check your answer
q1.check()

Correct

We’ve decided to add “logging” to our to_smash function from the previous exercise.

def to_smash(total_candies):"""Return the number of leftover candies that must be smashed after distributingthe given number of candies evenly between 3 friends.>>> to_smash(91)1"""print("Splitting", total_candies, "candies")return total_candies % 3to_smash(91)

Splitting 91 candies
1

What happens if we call it with total_candies = 1?

to_smash(1)

Splitting 1 candies
1

That isn’t great grammar!

Modify the definition in the cell below to correct the grammar of our print statement. (If there’s only one candy, we should use the singular “candy” instead of the plural “candies”)

def to_smash(total_candies):"""Return the number of leftover candies that must be smashed after distributingthe given number of candies evenly between 3 friends.>>> to_smash(91)1"""print("Splitting", total_candies, "candy" if total_candies == 1 else "candies")return total_candies % 3to_smash(91)
to_smash(1)

Splitting 91 candies
Splitting 1 candy
1

  1. pyhton学习总结:Booleans and Conditionals相关推荐

    1. pyhton学习实例demo

      pyhton学习实例demo 一行代码输出特定字符"Love"拼成的心形 一行代码打印九九乘法表 一行代码打印九九乘法表 一行代码计算出1-100之间的素数(两个版本) 一行代码输 ...

    2. 3.Booleans and Conditionals

      Booleans Python有bool类型数据,有两种取值:True 和 False. [1] x = True print(x) print(type(x)) True <class 'bo ...

    3. Pyhton学习——Day25

      #面向对象的几个方法#1.静态方法@staticmethod,不能访问类属性,也不能访问实例属性,只是类的工具包#2.类方法:@classmethod,在函数属性前加上类方法,显示为(cls)代表类, ...

    4. pyhton学习之找出单词的个数并进行排序

      在学习C的时候,我们经常会做这样的算法题来提高我们的编程能力,然而在python中使用封装好的类库,就可以简单完成. 需要的模块有:re :该模块可以通过一定的规则寻找目标   Counter主要是用 ...

    5. Pyhton学习笔记第一天(Python基本语句)

      Python学习笔记第一天 注释 行注释 块注释 输出语句 举一反三 标识符 举一反三 多行语句 结束语 注释 什么是注释,注释相当于备注的信息,也可以在调试代码的时候隐藏执行代码,但只适合新手.老手 ...

    6. Pyhton学习——Day3

      # li = [1,12,9,"age","alex"] # #列表表示为[]括起来 # #列表用","分割每个元素 # #列表中的元素可以 ...

    7. Pyhton学习——Day9

      #文件操作的其他方法 ###############################################################################文件的内置操作函数# ...

    8. day10 Pyhton学习

      一.昨日内容回顾 函数: 定义:对功能或者动作的封装 def 函数名(形参): 函数体 函数名(实参) return:  返回,当程序运行到return的时候,终止函数的执行 一个函数一定拥有返回值 ...

    9. pyhton学习,day1作业,用户名密码登录模块

      要求,通过用户名密码登录,登录错误3次,锁定用户名 1 # coding=utf-8 2 # Author: RyAn Bi 3 4 import os, sys #调用系统自己的库 5 6 acco ...

    最新文章

    1. 加拿大工程院院士于非:互联—— 从质量、能源、信息到智能
    2. 流程的python-什么时候学流畅的python合适?
    3. python语言if语句-Python中的if判断语句入门
    4. 23种设计模式C++源码与UML实现--桥接模式
    5. 解决listview中的textview设置了setMovementMethod导致onItemclick无效的问题
    6. 标准C程序设计七---66
    7. ListView缓存机制踩过的坑
    8. CSS3 动画 animation属性
    9. 《写给PHP开发者的Node.js学习指南》一2.2 预定义的PHP变量
    10. MFC列表控件(ListControl)
    11. Coursera机器学习课后作业Matlab代码(1~9周)
    12. Linux c学习--进程基础学习
    13. G - Power Strings
    14. 内外盘分仓跟单系统源码CTP程序化交易系统源码
    15. HTML5期末大作业:商城后台网站设计——网站商城后台通用模板(30页) 大学生后台模板网页作品商城网页设计作业模板 学生网页制作源代码下载
    16. TortoiseSvn执行SVN Commit一直显示Please wait...问题解决
    17. 公众号开发定时群发php,微信公众平台新增定时群发功能(新功能)
    18. Kaleao推出基于ARM的服务器Kmax
    19. 一款值得使用的,免费会议室管理软件【微信小程序和网页版都有】
    20. php 判断华为手机,一招教你如何看穿华为手机的内存类型

    热门文章

    1. OneDark-Pro-darker.json
    2. 计算机出现家庭组图标怎么删除,Win7系统如何删除资源管理器左侧导航栏“家庭组”图标...
    3. ubuntu无线优先上网
    4. win10的计算机管理用户和组在哪里,Win10专业版下管理用户和组的技巧
    5. linux 查看java端口_Linux之查看端口使用情况
    6. 目标跟踪评价指标-精度Pr、召回率Re
    7. Dart语法之可选参数{ } [ ]
    8. 如何写软文推广产品?教你用软文来介绍产品
    9. mes系统与Oracle对接,Oracle ERP系统与MES系统的接口分析与实现
    10. 电子书网站系统建设构想