Python實現猜數字遊戲

來源:互聯網
上載者:User

標籤:sdi   color   utf-8   blog   pyc   遊戲   自動   erro   out   

遊戲規則:

1.隨機產生1到100之間的整數
2.共有3次猜測機會,每次猜測如果不正確會提示大於或小於目標值,3次機會用完退出程式
3.三次機會,包含第3次機會如果猜中,提示使用者猜中
4.如果使用者輸入不合法需要有提示並重新輸入

代碼如下:

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time    : 2017-09-03 16:33# @Author  : Aixce# @Email   : [email protected]# @File    : 猜字謎.py# @Software: PyCharmimport randomprint("########  猜數字小遊戲   #########")print("#  1.開始遊戲  2.任意鍵離開遊戲  #")print("#    註:猜錯三次遊戲自動結束.   #")print("##################################")put = input("請輸入:")if put == "1":    number = 3    secret = random.randint(1, 100)    while number > 0:        temp = input("不妨猜一下我現心裡想的數字是幾:")        temp2 = temp.strip()        if temp2.isdigit():            temp1 = int(temp2)            if temp1 ==secret:                print("哼,我心裡想的數是%s,你居然猜中了,猜中也沒有獎勵^_^"%secret)            elif temp1 > secret:                print("我心想的數字比%s小,還是剩%s次機會"%(temp1,number-1))            else:                print("我心想的數字比%s大,還是剩%s次機會"%(temp1,number-1))        else:            print("Error:‘%s‘不是一個數字,請輸入一個整數" % temp)            number += 1        number -= 1else:    exit("離開遊戲成功!")print("遊戲結束!")

執行結果:

Python實現猜數字遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.