pyhton implements a simple guessing game

Simple implementation (two methods)

One

user=int( input("0(剪刀),1(石头),2(布)") )
import random
pc =(random .randint(0,2))
if user==0 and pc==2 or user==1 and pc==0 or user== 2 and pc==1:
    print("胜利")
elif user ==pc :
    print("平局")
else:
    print("败北")

two

#定义一个函数
#人输入
#计算机输入
#做比较
def user():
   user=int( input("0(剪刀),1(石头),2(布)") )
def pc():
    import  random
    pc = (random.randint(0, 2))
if __name__ == '__main__':
    y = user()
    x=pc()
    print(type (x ))
    if y  == 0 and x == 2 or y  == 1 and x == 0 or y  == 2 and x  == 1:
        print("胜利")
    elif y  == x :
        print("平局")
    else:
        print("败北")

Guess you like

Origin blog.csdn.net/weixin_47514459/article/details/109393089