初学Python-day8 案例2
作者:互联网
中奖率
1 import random 2 num = 123456 3 i = 1 4 while True: 5 win = random.randrange(100000, 999999) 6 if num == win: 7 print('中奖了') 8 break 9 i += 1 10 print(i)
运行结果:
中奖了 2438830
猜21点(离21越近的玩家,算赢)
1 import random, time 2 3 username1 = input('请输入玩家1名字:') 4 username2 = input('请输入玩家2名字:') 5 info = {username1: {'count': 0}, username2: {'count': 0}} 6 while True: 7 computerNum1 = random.randint(1, 10) 8 computerNum2 = random.randint(1, 10) 9 print('输出q退出游戏!') 10 userNum1 = input('输入一个数字(1-10):') 11 userNum2 = input('输入一个数字(1-10):') 12 if userNum1 == 'q' or userNum2 == 'q': 13 print('退出游戏!') 14 break 15 16 userSum1 = int(userNum1) + computerNum1 + computerNum2 17 userSum2 = int(userNum2) + computerNum1 + computerNum2 18 print('底牌为:', computerNum1, computerNum2) 19 if abs(userSum1 - 21) > abs(userSum2 - 21): 20 print('{}点数为:{}\n{}点数为{},{}获胜!'.format(username1, userSum1, username2, userSum2, username2)) 21 info[username2]['count'] += 1 22 else: 23 print('{}点数为:{}\n{}点数为{},{}获胜!'.format(username1, userSum1, username2, userSum2, username1)) 24 info[username1]['count'] += 1 25 26 print('进行下一局,正在洗牌') 27 time.sleep(2)
登录
1 import random 2 username = 'admin' 3 userpw = '123456' 4 5 while True: 6 yzm = random.randrange(1000, 9999) 7 loginName = input('请输入用户名:') 8 loginPw = input('请输入密码:') 9 loginYzm = int(input('{}请输入验证码:'.format(yzm))) 10 if yzm != loginYzm: 11 print('请输入正确的验证码!') 12 continue 13 if loginName != username: 14 print('当前用户不存在') 15 continue 16 if loginPw == userpw: 17 print('欢迎{}', username) 18 break 19 else: 20 print('密码错误!')
猜数字
num = 10 while True: try: userNum = eval(input('请输入你要猜的数字:')) if userNum == num: print('猜对了') break elif userNum > num: print('猜大了') else: print('猜小了') except: print('请输入正确的数字')
标签:username1,10,username2,day8,Python,random,初学,print,input 来源: https://www.cnblogs.com/liu-blogs/p/15226449.html