python的判断语句if

您所在的位置:网站首页 命运2幽梦之城日常挑战 python的判断语句if

python的判断语句if

2024-01-10 10:00| 来源: 网络整理| 查看: 265

练习一

题目: 1.从控制台输入要出的拳 —石头(1)/剪刀(2)/布(3) 2.电脑随即出拳 3.比较胜负 石头 胜 剪刀 剪刀 胜 布 布 胜 石头

结果

import random 1.从控制台输入要出的拳---石头(1)/剪刀(2)/布(3) player = int(input('请输入你要出的拳头: ---石头(1)/剪刀(2)/布(3)')) 2.让电脑随机出拳 computer = random.randint(1,3) print('玩家:%d,电脑:%d' %(player,computer)) if ((player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1)): print('玩家胜利!!!') elif player == computer: print('平局!!!') else: print('玩家输了!!!')

在这里插入图片描述输出结果 在这里插入图片描述

练习二

题目:

判断闰年? 用户输入年份year, 判断是否为闰年? ( year能被4整除但是不能被100整除 或者 year能被400整除, 那么就是闰年)

year = int(input('请输入年份')) if ((year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)): print('闰年') else: print('不是闰年')

在这里插入图片描述结果 在这里插入图片描述 在这里插入图片描述

练习三

题目: 随机选择一个三位以内的数字作为答案。用户输入一个数字,程序会提示大了或是小了

import random computer = random.randint(0, 1000) user = int(input('请给出你的答案:')) if (user > 999): print('请输入三位数字') exit()#当玩家没有按照规定的题目输入三位数字,则直接停止执行所有程序 print('电脑的数字:%d\n玩家给出的答案:%d' % (computer, user)) if (computer > user): print('小了') elif (computer


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3