初學python,用python寫了一個簡單的猜大小的小游戲
#!/usr/bin/env python #-*- coding:utf-8 -*- print "--------------測試--------------" temp = input("猜一下我心里想的是那個數 ") guess = int(temp) while guess != 8: temp = input("哎呀,猜錯了,請重新輸入吧:") guess = int(temp) if guess == 8: print "猜對了誒" print "哼,猜對了也沒有獎勵" else: if guess > 8: print "哥,大了大了" else: print "嘿,小了小了" print "游戲結束,不玩了,嘿嘿"
使用random模塊,產生一個隨機答案
#!/usr/bin/env python #-*- coding:utf-8 -*- import random secret = random.randint(1,10) print "--------------測試--------------" temp = input("猜一下我心里想的是那個數 ") guess = int(temp) while guess != secret: temp = input("哎呀,猜錯了,請重新輸入吧:") guess = int(temp) if guess == secret: print "猜對了誒" print "哼,猜對了也沒有獎勵" else: if guess > secret: print "哥,大了大了" else: print "嘿,小了小了" print "游戲結束,不玩了,嘿嘿" ~
