栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【习题一】用Python编写猜拳游戏

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【习题一】用Python编写猜拳游戏

目录

优点:

答案一:

答案二:

优点: 根据老师进度编写,小白一枚,只为记录每次进步,欢迎大家提出意见与建议。

答案一:
#用户
user = int(input('请输入:剪刀(0)、石头(1)、布(2):'))

if user == 0 :
    chinese = "剪刀"
elif user == 1:
    chinese = "石头"
else:
    chinese = "布"
print("你的输入为%s(%d)"%(chinese,user))

#电脑
import random     #调用随机库
computer=random.randint(0,2)
print("随机生成数字为%d"%computer)

#判断输赢
if user == 0 and computer == 0 :
    print("哈哈,这次不算再来一次吧!")
if user == 0 and computer == 1 :
        print("哈哈,你赢了!)")
if user == 0 and computer ==2 :
            print("哈哈,你输了!")
elif user == 1 and computer == 0 :
    print("哈哈,你赢了!")
if user==1 and computer == 1 :
        print("哈哈,这次不算再来一次吧!")
if user == 1 and computer == 2:
            print("哈哈,你输了!")
else :
 if user == 2 and computer ==0 :
        print("哈哈,你输了!")
if  user ==2 and computer == 1:
         print("哈哈,你赢了!")
if user ==2 and computer ==2:
                 print("哈哈,这次不算再来一次吧!")
答案二:

优化后的答案,真的是太妙了!!!竟然能想到取余,这是我的一位同学告诉我的方法,真的是太优美了!!!

#用户
user = int(input('请输入:剪刀(0)、石头(1)、布(2):'))

if user == 0 :
    chinese = "剪刀"
elif user == 1:
    chinese = "石头"
else:
    chinese = "布"
print("你的输入为%s(%d)"%(chinese,user))
len=3

#电脑
import random     #调用随机库
computer=random.randint(0,2)
print("随机生成数字为%d"%computer)

#判断输赢
if (user+2)%len == computer:
    print("哈哈,你赢了!")

elif (user+1)%len == computer:
    print("哈哈,你输了!")

else :
    print("哈哈,这次不算再来一次吧!")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号