1. 程式人生 > >大家一起學python-day2 -布林型別,基礎運算和基礎流程

大家一起學python-day2 -布林型別,基礎運算和基礎流程

'''
#布林型
#1.0,空元組,列表,字典,字串,none都屬於flase
print(bool(0))#整型
print(bool([]))#列表
print(bool(()))#元組
print(bool({}))#字典和集合
print(bool(''))#字串
print(bool(None))#None

#2.非零,非空列表,字典,字串,元組
print(bool(1))#整形
print(bool([1,1]))#列表
print(bool({'name':'word'}))#字典
print(bool({1,2,3,4,5,6,7,7}))
print(bool('sdad'))#字串
print(bool((1,2,3,4,)))#元組
#注:對於字典和集合(兩個都是大括號{}),空{}的type是字典型別!
print(type({}))
'''

# 3.運算子
a = 10
b = 20
print(a + b)  # 加法運算子
print(a - b)  # 減法運算子
print(a * b)  # 乘法運算子
print(a / b)  # 除法運算子
print(a ** b)  # 冪運算子
print(9 // 4)  # 地板除,向下取整數
print(9 % 4)  # 取餘數#可用這個判斷是否是奇數或者偶數

# 4.複合運算
# +=  --> a=a+b
# *=  --> a=a*b
# -=  --> a=a-b


# 5.比較運算子
print(10 > 0)  # 大於
print(10 != 20)  # 不等於
print(10 < 20)  # 小於
print(10 == 10)  # 等於
print(10 >= 10)  # 大於等於
print(10 <= 10)  # 小於等於

# 6.邏輯運算子
b = 10 > 4  # true
b1 = 10 > 2  # true
b2 = 2 > 3  # false
b3 = 10 < 5  # false
print(b and b1 and b2)
print(b or b1 or b2)
print(b1 or b2 or b3)
print(b2 or b3)
print(b)
print(not b)

a = 10
b = 'aa'
print(a and b)  # 當a為真,需要看後面的是否也為真,所以返回後面b
print(a or b)  # 有一個真的就可以,所以當a為真的時候,不用往後看,直接輸入a

#7.邏輯運算的優先順序
#not > and >or

#8.import random

money = 100
while True:
    print('1代表剪刀')
    print('2代表石頭')
    print('3代表布')
    player = input("請您輸入剪刀或者石頭或者布")
    if player.isdigit() == False:
        print("輸入錯誤!請重新輸入")
        continue
    else:
        player = int(player)
        computer = random.randrange(1, 3)
        print("電腦出的是", computer)
        if player == computer:
            print("平局,請重新再來")
        elif player == 1 and computer == 3 or player == 2 and computer == 1 or player == 3 and computer == 2:
            print("您贏了")
            money += 10
        else:
            print("你輸了")
            money -= 10
        print("您還有", money, '錢')
        if money == 0:
            print('太菜了!沒錢了!')


# a = 1
# while a < 10:
#     print(" "*(10-a),'*'*a)
#     print()
#     a += 1
'''
#求和
a = 1
b = 0
while a <= 100:
    b += a
    a += 1
print(b)
#求偶數
c = 1
d = 0
while c <= 100:
    if c % 2 == 0:
        d += c
    c += 1
print(d)
'''
# 求和平均值
# a = 1
# b = 0
# c = 0
# while a <= 5:
#     print("請輸入您的第", a,"個數:")
#     c = input()
#     c = int(c)
#     b = b + c
#     a += 1
# print("平均值:", b / 5)
# print("和:", b)

# 逆序輸出
#第一種方法
a = int(123/100)#百位
b = int((123-a*100)/10)#十位
c = int(123-int((123/100))*100-int((123-a*100)/10)*10)#個位
d = c*100 +b*10+a
print(d)
#第二種方法
a = '123'
b = a[2]+a[1]+a[0]
print(b)