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文件�?

其实还有很多,但我实在不行啦!

原创粉丝点击