#!/usr/bin/env python
# -*- coding: UTF-8 -*-
def instructions(): # 定义函数
"""Display game instructions."""
print(
"""
Welcome to the greatest intellectual challenge of all tome : Tic-Tac-Toe.
This will be a showdown between your human brain and my silicon processor.
You will make your move known by entering a number, 0 - 8. The number will
correspond to the board position as illustrated:
0 | 1 | 2
---------
3 | 4 | 5
---------
6 | 7 | 8
Prepare yourself, human. The ultimate battle is about to begin. \n
"""
)
def instructions(spp): # 可重载
print(spp)
def birthday(name, age = 0, sex = "man"): # 某个参数设置了默认值时,其后所有参数都需要设置默认值
print(name, age, sex)
def read_global():
print("From inside the local scope of read_global(), value is:", value) # 函数内部读取全局变量
def shadow_global():
value = -10 # 函数内部屏蔽全局变量
print("From inside the local scope of shadow_global(), value is:", value)
def change_global():
global value # 获取全局变量value的完全访问权
value = -10 # 函数内部修改全局变量
print("From inside the local scope of change_global(), value is:", value)
# 主程序
print("Here are the instructions to the Tic-Tac-Toe game:")
instructions()
print("Here they are again:")
instructions()
print("You probably understand the game by now.")
input("\n\nPress the enter key to exit.")
# 关键字实参可以不关心形参的顺序
birthday(age = 15, name = "Tom")
value = 10 # 全局变量
print("In the global scope, value has been set to :", value, "\n")
read_global()
print("Back in the global scope, value is still :", value, "\n")
shadow_global()
print("Back in the global scope, value is still:", value, "\n")
change_global()
print("Back in the global scope, vlaue is still:", value, "\n")
代码Python入门(八、函数)(转p3)
最新推荐文章于 2023-12-25 17:32:47 发布
这篇博客介绍了Python中的函数定义与使用,包括带有默认参数的函数、读取和修改全局变量的方法,以及如何通过函数重载显示游戏说明。
摘要由CSDN通过智能技术生成