【python】Ch4_对象类型与小小实战

学到对象类型的时候,必须得回顾一下 python 这门语言的基本特征(回头看 Ch1: https://blog.csdn.net/qq_37165755/article/details/79836920),并且一个概括的结论是:
    1. 程序由模块构成
    2. 模块包含语句
    3. 语句包含表达式

    4. 表达式建立,并处理对象

python 最大的优点就是可以一直专注在编程的内容里,而不用管理内存结构部署,内存分配,搜索和读取例程等这类 C 或 C++ 需要额外做的事情,这就是以“对象类型”为语言组成部分的好处。它更容易编写,更方便的扩展组件应用。


对象里面又包括了两类,一个是 python 本身就具备的内置对象,另一个是我们因为需要而另外创造的对象。要理解它们是怎么给程序员带来便利的事实之前,需要从内置对象重新理解 python 的核心数据类型包含了以下:

  1. 数字 number:整数 int,浮点数 float,虚数 complex
  2. 字符串 string:被“”或是‘’夹起来的中间内容
  3. 列表 list:用中括号 "[]"夹在中间,并且用“,”区隔数据的资料形态
  4. 字典 dictionary:用大括号 "{}" 夹在中间,用“,”区隔数据,并用“:”的右边定义左边的名字的资料形态
  5. 元组 tuple:用小括号 "()"夹在中间,并且用“,”区隔数据的资料形态
  6. 文件 file:例如 myfile = open('eggs', 'r')
  7. 其他:集合,类型,None,Bool

每个类型的对象都有其相对的 function 可以被使用,这些方法就是只能在使用过程中逐渐融入到脑子里的内容了,查询所有的功能的办法可以用 "dir(???)" ,或是 "help(???)" ,这样所有的 function 就会被打印出来,而上面这些类型的细节也将会逐步在后面的单元里面被介绍。老话一句,希望能够帮到充满热情却无从下手的人。


以下是初学的时候自己动手练习的两个小玩意儿:
1. 计算机工具(只能用来算两个元素彼此的加减乘除,如果输入的东西不是数字则报错)

def add(num1, num2):
    return num1 + num2

def sub(num1,num2):
    return num1 - num2

def mul(num1,num2):
    return num1 * num2

def div(num1, num2):
    return num1/num2

def calculator():
    gooncalc = True
    while gooncalc:
        validinput = False
        while not validinput:
            try:
                num1 = int(input('please type in the first number:'))
                num2 = int(input('please type in the second number:'))
                operation = int(input('1 for add, 2 for sub, 3 for mul, 4 for div'))
                validinput = True
            except:
                print('invalid input, please try again')
        if (operation == 1):
            print('calculating...')
            print(add(num1,num2))
        elif (operation == 2):
            print('calculating...')
            print(sub(num1,num2))
        elif (operation == 3):
            print('calculating...')
            print(mul(num1,num2))
        elif (operation == 4):
            print('calculating...')
            print(div(num1,num2))
        else:
            print('please enter 1-4 to calculate')

        goonornot = input('would you like to run another calculation? press Y to say yes\
        and press any other key to exit')
        if (goonornot != 'y'):
            goonornot = False
            break
        else:
            continue

calculator()

2. 猜拳游戏(让电脑随机得出结果,比较玩家输入的结果并最后给一个结论)
import random
import time

def game():
    while True:
        print('paper scissor stone! pick one to beat me...')
        a = input('Python is ready now! I go with...')
        b = ['paper','scissor','stone']
        c = ['stone','paper','scissor']
        d = ['scissor','stone','paper']
        if (b.count(a) == 0):
            print('it is not paper, scissor or stone. Please enter again.')
            continue
        while (b.count(a) == 1):
            if (a == 'paper'):
                autodecision = random.randint(0, 2)
                if (autodecision > c.index(a)):
                    print('Python goes with...' + c[int(c.index(a)) + 1])
                    print('comparing...')
                    time.sleep(1)
                    print('Python has won!')
                elif (autodecision == c.index(a)):
                    print('Python goes with...' + c[int(c.index(a))])
                    print('comparing...')
                    time.sleep(1)
                    print('We are equal this time!')
                elif (autodecision < c.index(a)):
                    print('Python goes with...' + c[int(c.index(a) - 1)])
                    print('comparing...')
                    time.sleep(1)
                    print('You are so lucky!')
            elif (a == 'stone'):
                autodecision = random.randint(0, 2)
                if (autodecision > d.index(a)):
                    print('Python goes with...' + d[int(d.index(a)) + 1])
                    print('comparing...')
                    time.sleep(1)
                    print('Python has won!')
                elif (autodecision == d.index(a)):
                    print('Python goes with...' + d[int(d.index(a))])
                    print('comparing...')
                    time.sleep(1)
                    print('We are equal this time!')
                elif (autodecision < d.index(a)):
                    print('Python goes with...' + d[int(d.index(a) - 1)])
                    print('comparing...')
                    time.sleep(1)
                    print('You are so lucky!')
            elif (a == 'scissor'):
                autodecision = random.randint(0, 2)
                if (autodecision > b.index(a)):
                    print('Python goes with...' + b[int(b.index(a)) + 1])
                    print('comparing...')
                    time.sleep(1)
                    print('Python has won!')
                elif (autodecision == b.index(a)):
                    print('Python goes with...' + b[int(b.index(a))])
                    print('comparing...')
                    time.sleep(1)
                    print('We are equal this time!')
                elif (autodecision < b.index(a)):
                    print('Python goes with...' + b[int(b.index(a) - 1)])
                    print('comparing...')
                    time.sleep(1)
                    print('You are so lucky!')
            break
        option = input('are you ready for the next round? \
			press any buttom to go on and press N to exit...')
        if (option == 'n'):
            print('see you next time')
            break
        else:
            continue

game()

可以直接把上面的代码放到编写环境里去执行,享受一下逻辑具象化的快感。

以下附上初学者启蒙影片来源:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值