莫烦python基础教程(七)

class类和input输入

class类


class 定义一个类

  • class 定义一个类,后面的类别首字母推荐以大写的形式定义,比如Calculator。
  • class可以先定义自己的属性,比如该属性的名称可以写为 name=’Good Calculator’。
  • class后面还可以跟def,定义一个函数。比如def add(self, x, y): 加法,输出print(x+y)。这里的self 是默认值。
class Calculator:   # 首字母要大写,冒号不能少
    name = 'Good Calculator'    # 该行为class的属性
    price = 18
    def add(self, x, y):
        print(self.name)
        result = x + y
        print(result)
    def minus(self, x, y):
        result = x - y
        print(result)
    def times(self, x, y):
        print(x * y)
    def divide(self, x, y):
        print(x / y)

>>> cal = Calculator()  # 定义自变量cal等于Calculator要加括号“()” ,cal=Calculator()否则运行下面函数的时候会出现错误,导致无法调用。
>>> cal.name
'Good Calculator'
>>> cal.price
18
>>> cal.add(10, 20)
Good Calculator
30
>>> cal.minus(10, 20)
-10
>>> cal.times(10, 20)
200
>>> cal.divide(10, 20)
0.5

class 类 init 功能


init

  • __init__可以理解成初始化class的变量,可以在运行时,给初始值附值。init这里的下划线是双下划线
  • 下列代码运行c=Calculator(‘bad calculator’, 18, 17, 16, 15),然后调出每个初始值的值。
class Calculator:   # 首字母要大写,冒号不能少
    name = 'Good Calculator'    # 该行为class的属性
    price = 18
    def __init__(self, name, price, height, width, weight):    # 注意,这里的下划线是双下划线
        self.name = name
        self.price = price
        self.h = height
        self.wi = width
        self.we = weight

>>> c = Calculator('bad calculator', 18, 17, 16, 15)
>>> c.name
'bad calculator'
>>> c.price
18
>>> c.h
17
>>> c.wi
16
>>> c.we
15
  • 设置属性的默认值,直接在def里输入即可:def __init__(self, name, price, height=10, width=14, weight=16), 三个有默认值的属性可以直接输出默认值,这些默认值可以在code中更改。比如c.wi=17再输出c.wi就会把wi属性值更改为17,同理可推其他属性的更改方法。
class Calculator:   # 首字母要大写,冒号不能少
    name = 'Good Calculator'    # 该行为class的属性
    price = 18
    def __init__(self, name, price, height = 10, width = 14, weight = 16):    # 后面三个属性设置默认值,查看运行
        self.name = name
        self.price = price
        self.h = height
        self.wi = width
        self.we = weight

>>> c = Calculator('bad calculator', 18)
>>> c.h
10
>>> c.wi
14
>>> c.we
16
>>> c.we = 17
>>> c.we
17

input 输入


input

  • variable=input() 表示运行后,可以在屏幕中输入一个数字,该数字会赋值给自变量。
a_input = input('please input a number:')
print('this number is:', a_input)

>>> 
please input a number:12
this number is: 12
  • 在下面代码中,需要将input() 定义成整型,因为在if语句中自变量 a_input 对应的是1 and 2 整数型。输入的内容和判断句中对应的内容格式应该一致
a_input = int(input('please input a number:'))  # 注意这里要定义一个整数型
if a_input == 1:
    print('This is a good one.')
elif a_input == 2:
    print('See you next time.')
else:
    print('Good Luck!')

>>> 
please input a number:1
This is a good one.

>>>
please input a number:2
See you next time.

>>> 
please input a number:3 or other number
Good Luck!

input扩展

  • 用input()来判断成绩:
score = int(input('please input your score:\n'))
if score >= 90:
    print('Congradulations! You get an A.')
elif score >= 80:
    print('You get a B.')
elif score >= 70:
    print('You get a C.')
elif score >= 60:
    print('You get a D.')
else:
    print('Sorry. You are failed.')

>>> 
please input your score:
100
Congradulations! You get an A.

猜你喜欢

转载自blog.csdn.net/faker1895/article/details/81835426
今日推荐