PYTHON函数
-
函数是指组织好的,可重复使用的,用来实现特定功能的代码段
-
语法:
def 函数名 (传入参数):
函数体
return 返回值
-
参数可以省略,返回值可以省略(省略时接受到None,也可以手动写return None)
-
None作为特殊的字面量,用于表示: 空、无意义;在if判断中None等同于False
# 例
def str_len(data):
for x in data:
print(x,end=" ")
a = "ABCDEF"
str_len(a)
- global关键字: 声明函数中的变量是全局变量;用于在函数内部修改全局变量的值
# ATM
money = 10000.00
name = "XIANG"
def main_menu():
print()
print("WELCOME TO ATM")
print("SEARCH MONEY [INPUT 1]")
print("SAVE MONEY INPUT 2]")
print("TAKE MONEY [INPUT 3]")
print("LEAVE [INPUT OTHER CHARACTER]")
input_data = input("PLEASE INPUT:")
if input_data == "1":
search_money()
elif input_data == "2":
save_money()
elif input_data == "3":
take_money()
return None
def search_money():
print()
print("-----------SEARCH MONEY-----------")
print(name+" SIR,YOUR MONEY :"+str(money))
input("INPUT ANY CHARACTER TO MAIN MENU:")
main_menu()
def save_money():
print()
print("-----------SAVE MONEY-----------")
save_money = float(input("INPUT YOUR SAVE AMOUNT:"))
global money
money += save_money
print("SUCCESSFUL!NOW YOUR MONEY:"+str(money))
input("INPUT ANY CHARACTER TO MAIN MENU:")
main_menu()
def take_money():
print()
print("-----------TAKE MONEY-----------")
save_money = float(input("INPUT YOUR TAKE AMOUNT:"))
global money
money -= save_money
print("SUCCESSFUL!NOW YOUR MONEY:" + str(money))
input("INPUT ANY CHARACTER TO MAIN MENU:")
main_menu()
main_menu()
标签:函数,PYTHON,money,MONEY,INPUT,print,input,data
From: https://www.cnblogs.com/Learn1ng/p/17086367.html