python脚本1

    技术2022-07-11  67

    脚本1: 循环

    import random xuanxiang = [‘石头’,‘剪刀’,‘布’] wins = [[‘石头’,‘剪刀’],[‘剪刀’,‘布’],[‘布’,‘石头’]]

    computer=random.choice(xuanxiang)

    a = ‘’’(0) 石头 (1) 剪刀 (2) 布 请选择(0/1/2):’’’ pwin = 0 cwin = 0

    while pwin < 2 and cwin < 2: #如果人机都没有赢沟两次继续 computer = random.choice(xuanxiang) i = int(input(a)) player = xuanxiang[i]

    print("your choice: %s, computer's choice: %s" % (player,computer)) if player == computer: print('\033[32m;1m平局\033[0m') elif [player,computer] in wins: print('\033[31;1mYOU WIN!!!\033[0m') pwin +=1 else: print('\033[31;1mYOU LOSE!!\033[0m') cwin +=1

    脚本2:break 循环:

    import random xuanxiang = [‘石头’,‘剪刀’,‘布’] wins = [[‘石头’,‘剪刀’],[‘剪刀’,‘布’],[‘布’,‘石头’]]

    computer=random.choice(xuanxiang)

    a = ‘’’(0) 石头 (1) 剪刀 (2) 布 请选择(0/1/2):’’’ pwin = 0 cwin = 0

    while 1: computer = random.choice(xuanxiang) i = int(input(a)) player = xuanxiang[i]

    print("your choice: %s, computer's choice: %s" % (player,computer)) if player == computer: print('\033[32m;1m平局\033[0m') elif [player,computer] in wins: print('\033[31;1mYOU WIN!!!\033[0m') pwin +=1 else: print('\033[31;1mYOU LOSE!!\033[0m') cwin +=1 if pwin == 2 or cwin == 2: break

    1加到100的偶数和 :

    脚本3: 0-100之间偶数的和:

    result=0 #定义变量,用于保存最宗结果 i=0 #定义一个累加计数器

    while i<100: i += 1 #1-100 2-101

    if i % 2 == 1: #0/1余数1,真 continue else: result += i

    print(result)

    while i<100: i += 1 #1-100 2-101

    if i % 2: #i%2的结果只有1或0,0为假,1为真 continue result += i

    print(result)

    脚本4: s1=‘hello’ for zifu in s1: print(zifu) nums = [10,20,30,40] for i in nums: print(i) users = (‘tom’,‘jerry’,‘zhangsan’) for i in users: print(i)

    info = {‘name’: ‘lisi’,‘age’: 20} for k in info: print(k,info[k])

    Processed: 0.015, SQL: 9