python實現猜數字和猜拳小遊戲

1.猜數字小遊戲python

#!/usr/bin/python
#-*-coding:utf-8-*-

import random
print "-------------------------------------"
print "歡迎使用aniuzaixian編寫的猜數字小遊戲"
print "+++++請閱讀遊戲規則+++++"
s = int(random.uniform(1,100))
print """遊戲規則:1.請輸入1-100(不包括100)之間的任意整數,/
         2.輸入0,退去遊戲!"""
print "------------------------------------"

name= raw_input("請輸入你的姓名:")
print "歡迎%s接受挑戰!"%name
m=-1
while s != m:

        str=raw_input("請輸入整數:")
        if(str.isdigit()):
                m=int(str)
                if m > s:
                        print "猜大了"
                        continue
                elif m == 0:
                        print "歡迎下次再試"
                        break
                elif m < s:
                        print "猜小了"
                        continue
        else:
                print "請輸入1-100之間的數字,不包含其餘字符串"
                continue
else:
        print "%s太棒了,猜對了"%name

 

2.猜拳小遊戲git

#!/usr/bin/env python
#-*-coding:utf-8 -*-
import random

print "歡迎使用aniuzaixian猜拳遊戲軟件!"
print """請閱讀遊戲規則:
         1.請輸入"石頭","剪刀","布"中的任意一個
         2.輸入 end,結束遊戲!
"""
name=raw_input("請輸入你的名字:")
print "歡迎%s猜拳"%name

com=0
per=0
draw=0
while True:
        s=int(random.randint(1,3))
        computer="電腦出拳"
        if(s==1):
                computer="剪刀"
        elif(s==2):
                computer="石頭"
        else:
                computer=""
        person=raw_input("請出拳:")
        list=["石頭","剪刀",""]
        if(person in list) and (person != "end"):
                if(computer == person):
                        print "平手了"
                        draw+=1
                        continue
                elif(computer=="石頭" and person=="剪刀") or (computer=="剪刀" and person=="") or (computer=="" and person=="石頭"):
                        print "電腦贏了"
                        com+=1
                else:
                        print "%s贏了"%name
                        per+=1
        elif(person not in list) and (person != "end"):
                print "你輸入有誤,請從新輸入!"
                continue
        else:
                sum = com + per + draw
                print "此次遊戲,%s共猜拳%d次,贏拳%d次,輸拳%d次,平手%d次" %(name,sum,per,com,draw)
                print "歡迎%s下次再玩!"%name
                break
相關文章
相關標籤/搜索