您的位置:首页 > 编程语言 > Python开发

python 手记9 〖笨方法学python习题30〗

2017-11-19 13:49 351 查看
如有意见或其他问题可在下方写下评论或加QQ:1693121186

欢迎一起讨论技术问题!

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


其实还有很多,但我实在不行啦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: