Python基礎入門學習(一)

Print

內置函數

BIF = = Built -in function

查看所有內置函數

dir(__builtins__)

在這裏插入圖片描述查看函數用法:

help(print)
#print用法
print('hello world ')
print(5+3)
print('hello'+' world')
print('hello world \n' * 8)

輸出:

hello world 
8
hello world
hello world 
hello world 
hello world 
hello world 
hello world 
hello world 
hello world 
hello world 

文字小遊戲

'''
-------------------------------------
文字小遊戲
-------------------------------------
'''
print('你好啊,小老弟')
temp = input("猜猜我心裏想得什麼數字?")
guess = int(temp)
if guess == 8:
    print("wocao,你是我肚子裏的蛔蟲嗎")
    print('猜對了也沒有獎勵哦')
else :
    print('猜錯了哦,遊戲結束')

輸出:

你好啊,小老弟
猜猜我心裏想得什麼數字?5
猜錯了哦,遊戲結束

變量和字符串

'''
-------------------------------------

變量和字符串
-------------------------------------
'''
#使用變量前要先賦值,
#可以包括字母、數字、下劃線,但不能數字開頭,字母大小寫不同
name_1 = '小老弟'
print(name_1)
name_2 = '大老弟'
print(name_2)
name_all = name_1+name_2
print(name_all)
one = 2
two = 6
three = one + two
print(three)

#字符串,單引和雙引均可
print('5'+'9')
print(5+9)
#轉義符號\
print('\'Good\'')
#原始字符串
print('Good\n')
print(r'Good\n')

輸出:

小老弟
大老弟
小老弟大老弟
8
59
14
'Good'
Good

Good\n

改進文字小遊戲

'''
-------------------------------------

改進文字小遊戲
引入random模塊
-------------------------------------
'''
#導入模塊

import random

#生成隨機整數
secret = random.randint(1,10)

#條件分支
# > < >= <= == != 

print('你好啊,小老弟')

temp = input("猜猜我心裏想得什麼數字?")
guess = int(temp)
count  = 0
while(count < 5):
    if guess == secret:
        print("wocao,你是我肚子裏的蛔蟲嗎")
        print('猜對了也沒有獎勵哦')
        break
    elif  guess < secret:
        print('猜錯了哦,猜得太小了')
    elif  guess > secret:
        print('猜錯了哦,猜得太大了')
    temp = input("來吧,繼續猜: ")
    guess = int(temp)
    count = count +1

print('遊戲結束哦')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章