# Warm Up !!! # 簡單猜拳小遊戲 ###### tags: `python` `遊戲` `猜拳` > Feel free to play around. :video_game: > Anaconda3 : jupyter notebook > ==Fat-cat-cat== Edit in 2020/3/11 --- ## :memo: Where do I start? ### Step 1: Open juypter notebook - [x] New -> python3 - [x] Change your filename(原本是Untitled) - [x] Relax for the next 30 min! :rocket::rocket::rocket: --- ### Step 2: Something new in python I hope you have learned some coding skill before! If not , it's ok :heart_eyes: **Here' some tips for you guys** :arrow_down: - [x] **python doesn't support "i++" but "i=i+1" is :ok:** - [x] **No ";" in the end of line** ---- :::info :bulb: **Hint:** You can also apply styling from the toolbar at the top :arrow_upper_left: of the editing area. ![](https://i.imgur.com/W0znH8G.jpg) ::: > ***shift + enter > can help you run the code!!!*** --- ### Step 3: Start Programing! >Skill used in this program:arrow_down: - [x]while loop - [x]if - elif - else - [x]random function - [x]print with 'string' :::info :pushpin: Want to learn more? ➜ [ Map Tutorials](https://hackmd.io/@mumu-0/HJzlhx-l4) ::: ---- ## If you want to know more about me! | Features | Link | | ----------------- |:----------------- | | Blogger(movie) | [:link:][movie] | | Blogger(programing)| [:link:][lab] | | Youtube | [:link:][youtube] | | Facebook | [:link:][fb]| [movie]: https://fatcatcat-movie.blogspot.com [youtube]: https://hackmd.io/c/tutorials/%2Fs%2Fhackmd-it [lab]: https://fatcatcat-lab.blogspot.com [fb]:https://www.facebook.com/mumuQQ/ ---- ```python= import random print("猜拳遊戲! 1 = 剪刀/ 2 = 石頭/ 3 = 布/ 4 = 不玩了") name =['哈','剪刀','石頭','布'] i = 0 ``` * import random package in order to use the random function * we use numbers to represent 剪刀/ 石頭/ 布/ 不玩了 * we use a 'list type' to include'剪刀','石頭','布' * '哈' 的部分會在之後講解 ---- ```python= import random print("猜拳遊戲! 1 = 剪刀/ 2 = 石頭/ 3 = 布/ 4 = 不玩了") name =['哈','剪刀','石頭','布'] i = 0 while (True): i=i+1 num = random.randint(1,3) guess = int(input("請輸入你猜的數字:")) if guess > 3 or guess < 1: if guess == 4: break else: print("請輸入1~3!") continue ``` * 'i' count number of times you play the game * while(True) formed a Infinite loop * 'if' statement judge whether the input is legal(1~3) ---- ```python= import random print("猜拳遊戲! 1 = 剪刀/ 2 = 石頭/ 3 = 布/ 4 = 不玩了") name =['哈','剪刀','石頭','布'] i = 0 while (True): i=i+1 num = random.randint(1,3) guess = int(input("請輸入你猜的數字:")) if guess > 3 or guess < 1: if guess == 4: break else: print("請輸入1~3!") continue ``` * randint(a,b) -> get a random number(>=a and <=b) * 不玩了就使用break 跳出迴圈 * continue 會忽略下方程式碼而重新開始迴圈 --- - 猜拳 diagrams ```sequence Note left of 剪刀(1): 我方 剪刀(1)->石頭(2): +1 石頭(2)->布(3): +1 布(3)->剪刀(1): -2 Note right of 布(3): 敵方 ``` 輸的條件 :-1: : 不能平手(我方數字=敵方數字) 敵方數字比我方多1 or 我出布敵方出剪刀 當然也可以已贏的條件去寫 :100: ---- ```python= if guess > 3 or guess < 1: ...(省略顯示,但是實際上要寫,上方已經展示過) elif guess == num: print("你出的是:%s" %name[guess]+"\n" + "我出的是:%s" %name[num]) print("平手!\n") elif (guess-num)==-1 or (guess-num)==2 : print("你出的是:%s" %name[guess]+"\n" + "我出的是:%s" %name[num]) print("你輸了~\n") else: print("你出的是:%s" %name[guess]+"\n" + "我出的是:%s" %name[num]) print("你贏了~\n") print("你總共猜了%d" %(i-1) + "次",end = '') print(",古德掰...") ``` * %name[guess] 加上 %s 可以顯示出我方'剪刀' or '石頭' or '布' * %name[num] 加上 %s 可以顯示出敵方'剪刀' or '石頭' or '布 ---- ```python= else: print("你出的是:%s" %name[guess]+"\n" + "我出的是:%s" %name[num]) print("你贏了~\n") print("你總共猜了%d" %(i-1) + "次",end = '') print(",古德掰...") ``` * 還記得'哈'嗎? python list 裡面的索引值是從0開始的,所以第一個索引值是0 ,放一個無意義的哈字來代表,而第二個索引值是1,也就是代表剪刀的數字,以此類推 * (i-1) 是因為最後一次迴圈是不玩了,不算是有猜拳,所以次數-1 --- 成果 ---- ![](https://i.imgur.com/s0BXnet.jpg) ---- - 複習按這邊 - 簡報的話按不了:) [ToC]
{"title":"Warm Up !!!","metaMigratedAt":"2023-06-15T05:07:52.944Z","metaMigratedFrom":"Content","breaks":true,"contributors":"[{\"id\":\"6bb13895-9bc1-441a-8b8f-cc9578e3b5bc\",\"add\":6486,\"del\":2449}]"}
    771 views