python 循环与判断,实例和小游戏练习。

逻辑判断

if...else 结构的作用:如果..条件是成立,就做....: 反之,就做.....

def account_login():
    password = input("请输入密码:")
    if password == '123456':
        print('输入正确')
    else:
        print('输入错误请重新输入')
    account_login()
account_login()

for循环做的的事情概况为一句话为:于...其中的每个元素,做什么事情。。

for i in range(1,10):
	for j in range(1,10):
		
		print('{}*{}={}'.format(i,j,i*j))
#运行结果为乘法口诀表

while循环:只要...条件成立,就一直做

conut = 0
while True:

    for i in range(1,10):
        files = open('C:\\Users\\yao\\Desktop\\' + str(i) + '.txt','w')
        files.close()
        conut += 1
        if conut > 11:
            break
#执行完毕后桌面上生成0-9数字命名的txt的文件

猜筛子大小的游戏

设定一个生成3个小于7随机数,求3个随机的数之和,将和进行大小比对。用户输入大小后,与函数得的结果进行对比。

import random

def roll_dice(numbers=3,points=None):
    if points is None:
        points = []
    while numbers > 0:
       point = random.randrange(1,7)
       points.append(point)
       numbers -= 1
    return points
def roll_result(total):
    isbig = 11 <= total <= 18
    issmall = 3 <= total <= 10
    if isbig:
        return 'big'
    elif issmall:
        return 'small'
def start_game():
    print("游戏开始")
    abc = input('big or small:')
    total = roll_dice()
    totals = sum(total)
    jieguo = roll_result(totals)
    youwin = jieguo == abc
    if youwin:
        print("你赢了")
    else:
        print('你输了')
    start_game()
start_game()

在原来的基础上再加上赌注金额

import random

def roll_dice(numbers=3,points=None):
    if points is None:
        points = []
    while numbers > 0:
       point = random.randrange(1,7)
       points.append(point)
       numbers -= 1
    return points
def roll_result(total):
    isbig = 11 <= total <= 18
    issmall = 3 <= total <= 10
    if isbig:
        return 'big'
    elif issmall:
        return 'small'
def start_game():
    abc = input('big or small:')
    total = roll_dice()
    totals = sum(total)
    jieguo = roll_result(totals)
    print(jieguo)
    print(abc)
    youwin = jieguo == abc
    '''if youwin:
        print("你赢了")
    else:
        print('你输了')'''
    return youwin
#gr = start_game()
#print(gr)


def bingo():
    print('您有1000块钱本金,祝您玩的愉快')
    rmb = 1000

    rmb1 = int(input("请选择下注:"))
    if rmb1 > 1000:
        print('你没有这么多钱嘚瑟什么?')
        return 
    print("游戏开始")

    cs = start_game()
    print(cs)
    if cs  == True:
        rmb += rmb1
        print(rmb)

    else:
        rmb = rmb -+ rmb1
        print(rmb)
    while True:
        if rmb <= 0:
            break
        else:
            bingo()
bingo()

 

你可能感兴趣的:(python)