python 手记9 〖笨方法学python习题30〗
2017-11-19 13:49
351 查看
如有意见或其他问题可在下方写下评论或加QQ:1693121186
欢迎一起讨论技术问题!
习题重点
elif——elif相当于是否则如果的意思(后面还需要加上条件,跟if一样的)
else——else相当于是否则的意思
可以说这些命令很像文字游戏里的分支剧情,就是给你几个选项让你点,每个都会出现不同的情况,很像吧?
So,我才没加注释,如果想看注释下来吧!
就是这样的,我也有点不懂,还是问别人的。
大胆设想:
我在打代码时想到了一个问题,if语句下面的内容可不可以换成其他的函数,于是我就开始了
其实还有很多,但我实在不行啦!
欢迎一起讨论技术问题!
people = 30 cars = 40 buses = 15 if 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,否则打印下面 4000 的内容 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 = 1111 b1 = 4000 print "%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 argv script, 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') #打开用户输入的文�?0 print "Truncating the file. Goodbye!" #打印 target.truncate() #清空该文件,反正你创建的也是新文�? print "Now I'm going to ask you for three lines." #打印 line1 = raw_input("line 1: ") #让用户输入值然后赋给变量line1 line2 = raw_input("line 2: ") #让用户输入值然后赋给变量line2 line3 = raw_input("line 3: ") #让用户输入值然后赋给变量line3 line4 = raw_input("line 4: ") #让用户输入值然后赋给变量line4 print "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 it target.close() #将刚刚输入的字符串都保存在text.txt文件�?
其实还有很多,但我实在不行啦!
相关文章推荐
- python 手记11 『笨方法学python 习题32』
- python 手记5 〖笨方法学python习题20〗
- python 手记7 〖笨方法学python习题22〗
- python 手记2 〖笨方法学python习题17〗
- python 手记8 〖笨方法学python习题29〗
- python 手记10 『笨方法学python 习题31』
- 笨方法学Python 习题 30: Else 和 If
- python手记(游戏) 笨方法学python习题36【持续更新中】
- python 手记6 〖笨方法学python习题21〗
- 笨方法学python 习题27(逻辑之前的内容)习题28(布尔)29、30、31
- python 手记4 〖笨方法学python习题19〗
- 笨方法学习Python-习题30: Else 和 If
- 笨方法学习Python-习题40: 字典, 可爱的字典
- 笨方法学习Python-习题10:那是什么?
- 笨方法学习Python-习题11:提问
- 笨方法学python 习题41(外星人游戏)
- 笨方法学Python习题45—你来制作一个游戏
- 笨方法学Python习题50—你的第一个网站
- 笨方法学python 习题12 需要重新看的一节
- 笨方法学Python 习题 17: 更多文件操作