《Python程式設計快速上手》(三)Python函式

2021-08-22 19:16:17 字數 3026 閱讀 4801

def hello():    #def語句,定義了乙個名為hello()的函式

print('howdy!') #函式體

print('howdy!!!') #函式體

print('hello there!') #函式體

hello() #呼叫

hello() #呼叫

hello() #呼叫

def hello(name):    #name為hello()函式的引數

print('hello '+ name)

hello('saber')

hello('arther')

【例】print()函式中的end,sep

【4條法則區分乙個變數是處於區域性作用域還是全域性作用域】

def spam():

global eggs

eggs = 'spam' #全域性變數

print(eggs)

def bacon():

eggs = 'bacan' #區域性變數

print(eggs)

def ham():

print(eggs) #全域性變數

#猜數字

#collatz序列

def clooatz(number):

if number%2==0:

print(str(number//2))

return number//2

else:

print(str(3*number+1))

return 3*number+1

try:

number = int(input())

while number!=1:

number=clooatz(number)

except valueerror:

print('your input must be intger!')

Python快速上手(三)

這一節總結一下python在編碼當中的一些需要注意的地方 一.常用 1.print語句 在螢幕上橫向輸出指定的字元,如 print hello world 在互動式環境當中 是提示符,不是 的一部分。多個語句可以用逗號 隔開。如 print hello world 2.if語句 age 20 注意 ...

Python程式設計快速上手 實踐專案

例如,字典值 意味著玩家有1條繩索 6個火把 42枚金幣等。寫乙個名為displayinventory 的函式,它接受任何可能的物品清單,並顯示如下 inventory 12 arrow 42 gold coin 1 rope 6 torch 1 dagger total number of ite...

《Python遊戲程式設計快速上手》 導讀

在寫作本書的過程中,我意識到,像python這樣的現代語言使得程式設計更加容易,並且為新一代的程式設計師提供了更多的功能。python擁有平緩的學習曲線,而且是供專業程式設計師使用的一種正規語言。目前的程式設計書籍大多分為兩種型別。第一種,與其說是教程式設計的書,倒不如說是在教 遊戲製作軟體 或教授...