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('游戏结束哦')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章