开始跟着视频学python,第五天mark【与或非、大于小于等于】

今天视频开始位置66:40
逻辑运算符。#就是与、或、非什么的。

has_high_income = True
has_good_credit = False

if has_good_credit and has_high_income:   #AND  与
    print('eligible for loan')
else:
    print('no')

输出为:

no

not 非

has_high_income = True
has_bad_credit = False

if has_high_income and not has_bad_credit:
    print('eligible for loan')
else:
    print('no')

小知识:

#优先级not>and>or
#and中含0,返回0; 均为非0时,返回后一个值
如3 and 4,返回4
#or中, 至少有一个非0时,返回第一个非0

大于 >
小于 <
等于 ==
大于等于 >=
不等于 !=

小练习:
如果名字小于3个字符,输出 太短
如果名字 大于20字符,输出太长
其他,输出 好名字

name = input('please input your name: ')    # input() 函数接受一个标准输入数据,返回为 string 类型
a = len(name)
if a > 20:      #也可直接  if len(name) > 20
    print("too long")
elif a <3:
    print("too short")
else:
    print("good name")

温馨提示:input输入返回的是字符串,如果是想输入数字进行计算的话需要用int()将其转换,而且int()不能转换字母。

练习:
输入体重,再输入单位,输出另一单位体重
如输入 70 ,再输入kg ,输出 140jin
实现如下:

weight = int(input("may i konw your weight number?  "))   #此处要求输入数字,其实应该加上防错输程序
danwei = input("kg or jin ?  ")
if danwei == 'kg':
    print(f"your weight is {weight * 2} jin.")
else:
    print(f"your weight is {weight / 2} kg.")

猜你喜欢

转载自blog.csdn.net/weixin_42944682/article/details/105562093