iT邦幫忙

2022 iThome 鐵人賽

DAY 20
0
自我挑戰組

轉職AI軟體工程師的自我學習分享筆記系列 第 20

Python 小遊戲實作: 終極密碼- (猜1個1~100的數字)

  • 分享至 

  • xImage
  •  

前情提要:

經過一連串~ brain storming, debug, paper studying, writing articles, positive of covid... hahaha jk lmao /images/emoticon/emoticon37.gif/images/emoticon/emoticon39.gif

However, I realised that my health is the piority! As a life where you have the energy to focus on everything else you want and dream of.

According to Petofi Sandor's poem in 1847, Life is dear, love is dearer. Both can be given up for freedom. Well... I only have life ~ so it's a precious tho!/images/emoticon/emoticon05.gif

So, let's have a break for coding some simple python games.
Those games are my first side project, which also my self-study of Python before I start the AI and Big Data program.

I learned it from the "小象學堂" which is a wechat account (kinda like wechat python learning robot ?!) that I have given from my brother. (p.s. My siblings were engineer before I be an AI RD/PM... but I'm the only one, who still alive in this field/images/emoticon/emoticon02.gif ... well life is hard... it's another story~ haha LOL, let come back to the article /images/emoticon/emoticon33.gif)

小遊戲介紹:

https://ithelp.ithome.com.tw/upload/images/20220930/201516810IkP4tQnGq.png
終極密碼- (猜1個1~100的數字) 是一項團體遊戲,遊戲可以由三人或三人以上以上進行。遊戲中一人做莊,給定一個數字範圍,再從中選出一個自然數,此數字稱為「密碼」,不能讓其他參與遊戲者得知。而遊戲參與者輪流猜測數字。每猜一個數,莊家就要告知遊戲者該數字是大於或小於密碼,直至密碼被猜中。

舉例如下:

猜測的範圍介於 1-100,終極密碼為46,兩個人玩,猜中贏,甲猜83,範圍變1-82,乙猜24,範圍變25-82,甲猜35,範圍變36-82,乙猜67,範圍變36-66,甲猜52,範圍變36-51,乙猜45,範圍變46-51,甲猜48,範圍變46-47,乙猜46,乙獲勝。

  • 小叮嚀: 此遊戲專案需輸入1~100的數字喔!

實作完整程式碼(如下):

#猜數字
import random
ans = random.randint(1,100)
guess = 0
while guess != ans:
    guess = int(input("請猜測一個1-100間的數字 : "))
    if guess > ans:
        print("要再小一點")
    elif guess < ans:
        print("要再大一點")
    else:
        print("恭喜答對")

開始來玩啦~ /images/emoticon/emoticon42.gif

哇~這次猜得有點久才猜到~ 在半夜自己跟自己玩,的小確幸~ 哈哈! /images/emoticon/emoticon01.gif/images/emoticon/emoticon02.gif
https://ithelp.ithome.com.tw/upload/images/20220930/20151681cITD8c2TXV.png


上一篇
Python 小遊戲實作: 1A2B
下一篇
Python 繪圖: 分型樹的繪製 (畫一大堆圖~p.s.還可以for表白用喔!)
系列文
轉職AI軟體工程師的自我學習分享筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言