python 手记9 〖笨方法学python习题30〗
来源:互联网 发布:数据之巅 电子书 编辑:程序博客网 时间:2024/06/11 15:12
如有意见或其他问题可在下方写下评论或加QQ:1693121186
欢迎一起讨论技术问题!
people = 30cars = 40buses = 15if cars > people: print "We shoule take the cars."elif cars < people: print "We shoule not take the cars."else: print "We can't decide."if buses > cars: print "That's too many buses."elif buses < cars: print "Maybe we could take the buses."else : print "We still can't decide."if people > buses: print "Alright, let's just take the buses."else: print "Fine, let's stay home then."
习题重点
- elif——elif相当于是否则如果的意思(后面还需要加上条件,跟if一样的)
- else——else相当于是否则的意思
可以说这些命令很像文字游戏里的分支剧情,就是给你几个选项让你点,每个都会出现不同的情况,很像吧?
So,我才没加注释,如果想看注释下来吧!
people = 30 #创建变量cars = 40 #创建变量buses = 15 #创建变量if cars > people: #遇到True就打印下面的内容 print "We shoule take the cars."elif cars < people: #如果cars小于people就跳过,否则打印下面的内容。 print "We shoule not take the cars."else: #以上的True条件都不行,否则就打印下面内容 print "We can't decide." if buses > cars: #如果buses大于cars,就打印下面的内容。 print "That's too many buses."elif buses < cars: #如果buses小于cars,否则打印下面的内容 print "Maybe we could take the buses."else: #上面两个命令被执行了,如果没。就打印下面的内容 print "We still can't decide."if people > buses: print "Alright, let's just take the buses."else: print "Fine, let's stay home then."
就是这样的,我也有点不懂,还是问别人的。
大胆设想:
我在打代码时想到了一个问题,if语句下面的内容可不可以换成其他的函数,于是我就开始了
联想“写入文件”
# -*- coding: utf-8 -*- a1 = 1111b1 = 4000print "%s, %s" % (a1, b1)if a1 < b1: print "The Numbers are really big, but then I'm going to make you crazy!Enter the text!"from sys import argvscript, filename = argv #将argv解包,然后将值依次赋于左边的的值不能讲script丢掉,然后后面的参数随你改print "We're going to erase %r." % filename #打印从argv解包解出的变量filename�?r大家都应该知�?print "If you don't want that, hit CTRL-C (^C)." #打印,后面的“CTRL-C(^C)”不要管print "If you do want that, hit RETURN." #打印raw_input("?") #raw_input指令让用户输入值,(里面是给用户的提示�?print "Opening the file..." #打印target = open(filename, 'w') #打开用户输入的文�?0print "Truncating the file. Goodbye!" #打印target.truncate() #清空该文件,反正你创建的也是新文�?print "Now I'm going to ask you for three lines." #打印line1 = raw_input("line 1: ") #让用户输入值然后赋给变量line1line2 = raw_input("line 2: ") #让用户输入值然后赋给变量line2line3 = raw_input("line 3: ") #让用户输入值然后赋给变量line3line4 = raw_input("line 4: ") #让用户输入值然后赋给变量line4print "I'm going to write these to the file." #打印 I‘m going to write these to the file."lines = "%s\n%s\n%s\n%s\n" % (line1, line2, line3, line4)target.write(lines)print "And finally, we close it." #打印And finally, we close ittarget.close() #将刚刚输入的字符串都保存在text.txt文件�?
其实还有很多,但我实在不行啦!
阅读全文
0 0
- python 手记9 〖笨方法学python习题30〗
- python 手记2 〖笨方法学python习题17〗
- python 手记3 〖笨方法学python习题18〗
- python 手记4 〖笨方法学python习题19〗
- python 手记5 〖笨方法学python习题20〗
- python 手记6 〖笨方法学python习题21〗
- python 手记7 〖笨方法学python习题22〗
- python 手记8 〖笨方法学python习题29〗
- python手记12 〖笨方法学python习题34〗
- python 手记10 『笨方法学python 习题31』
- python 手记11 『笨方法学python 习题32』
- 笨方法学python 习题35修改
- 笨方法学Python 习题 11: 提问
- 笨方法学Python 习题 41: 物以类聚
- 笨方法学Python 习题0
- 笨方法学python习题13、14
- 笨方法学python 习题16
- 笨方法学Python 习题 30: Else 和 If
- 电路试验
- 使用C3P0时,数据库链接死锁的解决
- LeetCode 解题
- 55.Jump Game
- three.js 画一个旋转的立方体
- python 手记9 〖笨方法学python习题30〗
- 教程篇(5.4) NSE4 08. 显式代理 ❀ 飞塔 (Fortinet) 网络安全专家
- java 自我知识总结(十八)final finalize finally
- 如何写一个RPC框架(六):负载均衡
- C#基础之静态类
- $.ajax()方法详解
- SpringBoot与Thymeleaf整合
- 为什么不能打断正在工作的程序员?
- 程序员相关笑话,你看懂几个?