【原创】python学习笔记(6)--《笨办法学python》,函数

一 函数,脚本,变量的类比

function:定义一个代码段block,命名为函数名,可以接受参数(var)

方法:变量的函数是方法    var.func()

script:      定义一个整个代码---代码文件,命名为脚本文件名,可接受参数argv,

var:      定义一个变量,命名为变量名,

module: 一个定义好的大段代码文件?比script更大?

二 函数带参数调用尝试

print 函数好像所有的内容只打印1次,如果之前打印了某几行,打印全部的时候的,那几行就不再被打印?

.readline() 据说是读的光标处的1行,连续的读,光标会随之移动,所以连续调用.readline()就显得像逐行打印一样的效果

# -*- coding:utf-8 -*-
#函数

from sys import argv
script,input_file=argv

def print_func(f):
	print(f.read())

def rewind(f):
	f.seek(0)

def print_a_line(line_count,f):
	print line_count,f.readline()

current_file=open(input_file)
print("let's print the whole file %s ") %input_file
print_func(current_file)

print("let's rewind")
rewind(current_file)

print current_file.readline()
print current_file.readline()           #readline()看起来只是每输入一次,依次打第1行。。。而且打完不显示了

"""
print("let's print three lines")
current_line=1
print_a_line(current_line,current_file)

current_line=1+current_line
print_a_line(current_line,current_file)

current_line=1+current_line
print_a_line(current_line,current_file)

"""

三  理解:函数定义与函数调用

下面这个例子,是根据书上的稍微改写的

(1) python是解释型语言,脚本性,胶水性语言,不是C++那种编译性的语言,

          是一边允许一边解释的,而没在运行的也就不会去解释。

          所以函数的定义内def 内,除了语法错误,其他运行时才会体现的错误是不会被提前检查出来的

         并且,理论上,def内的内容,可以认为都不算这个脚本:主程序的的一部分

(2)脚本的第1行实际可以认为是 print 另外就是主函数调用   start() 这两句,其他都是定义和被调用部分。

(3)现在 if or的用法还没搞清楚 ???

(4)现在还不熟悉,限制输入,或者规整输入,避免出错的方法?

      现在就是用 if 判断不符合要求,末尾再调回函数开头,形成这样的强制循环

# -*- coding:utf-8 -*-
from sys import exit

def start():
	print "you are now in a dark room"
	print "on your right side is a red door"
	print "on your left side is blue door"
	print "make your choice:red or blue?"
	user_choice=raw_input("you can only choose red or blue!>>")

	if user_choice=="red":      # "red" or "RED" 居然会导致选择都选前一支?
		gold_room()
	elif user_choice=="blue" :	#好像有办法可以忽略大小写,判断blue字符
		bear_room()
	else:
		print("you must make a choice")
		start()

def dead(reason):
	print reason,",haha good job ^ ^"
	exit(0)

def gold_room():
	print("this a big room full of golds,haha,but gold is too heavy to carry to much")
	print("how much gold do you want to take?")
	gold_take=raw_input("enter a numble>>")

	if isinstance(int(gold_take),int):			#判断数据类型int list dict
		gold_take_num=int(gold_take)			#当玩家数据类型输入不对,py本身会报错,这是个问题
	else:
		dead("man,you must first learn to type a numble!not others")

	if gold_take_num<=999999:
		print("you are lucky dog,take your gold and leave! ~ ~!")
	else:
		dead("you are too greedy,a snake in gold bite you to dead")

def bear_room():
	print("woo... a big bear is in the room!")
	print("what do you want to do?")
	print "A beat the bear;B talk with the bear and try to make friends"
	bear_chooice=raw_input("you can only enter A or B>>")
	if bear_chooice=="A" or "a":
		dead("how can you think you can fight a bear? the bear beat you to death!")
	elif bear_chooice=="B" or "b":
		print("lucky dog,the bear likes you ,you are friends now~ ^ ^ 666666")
	else:
		print("you can only enter A or B!")
		bear_room()

print("now let us play a game")	
start()


	
	

猜你喜欢

转载自blog.csdn.net/xuemanqianshan/article/details/82941029
今日推荐