From 250334f77796e50143352a7f50191784d07ed8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?python=E5=9F=B9=E8=AE=AD=E9=BB=84=E5=93=A5?= <1465376564@qq.com> Date: Tue, 10 Feb 2015 16:04:21 +0800 Subject: [PATCH] =?UTF-8?q?=20=E5=89=AA=E5=88=80=E7=9F=B3=E5=A4=B4?= =?UTF-8?q?=E5=B8=83=E5=B0=8F=E4=B9=A0=E9=A2=98=E4=B8=89=E7=A7=8D=E8=AF=AD?= =?UTF-8?q?=E8=A8=80python2=E3=80=81php=E3=80=81go=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jdstb.md | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 jdstb.md diff --git a/jdstb.md b/jdstb.md new file mode 100644 index 0000000..8f88dd8 --- /dev/null +++ b/jdstb.md @@ -0,0 +1,144 @@ + 剪刀石头布小习题三种语言python2、php、go代码 + 学习编程不只是学习语法、学习计算思维、编程思路才是重点。 + 参加黄哥python培训班、教会从只能看懂玩具代码过度到自己能写代码解决问题。 + [python远程视频培训班](https://github.com/pythonpeixun/article/blob/master/index.md) + + + # coding:utf-8 + """ + python核心编程6-14习题的解题思路 + 设计一个"石头,剪子,布"游戏,有时又叫"Rochambeau",你小时候可能玩过,下面是规则. + 你和你的对手,在同一时间做出特定的手势,必须是下面一种手势:石头,剪子,布.胜利者从 + 下面的规则中产生,这个规则本身是个悖论. + (a) 布包石头. + (b)石头砸剪子, + (c)剪子剪破布.在你的计算机版本中,用户输入她/他的选项,计算机找一个随机选项,然后由你 + 的程序来决定一个胜利者或者平手.注意:最好的算法是尽量少的使用 if 语句. + python培训 黄哥所写 python2 + """ + + import random + guess_list = ["石头", "剪刀", "布"] + win_combination = [["布", "石头"], ["石头", "剪刀"], ["剪刀", "布"]] + while True: + computer = random.choice(guess_list) + people = raw_input('请输入:石头,剪刀,布\n').strip() + if people not in guess_list: + people = raw_input('重新请输入:石头,剪刀,布\n').strip() + continue + if computer == people: + print "平手,再玩一次!" + elif [computer, people] in win_combination: + print "电脑获胜!" + else: + print "人获胜!" + break + + +##php语言实现 + + + + +##go语言实现 + + package main + + // 将python习题剪刀石头布修改为go语言的代码 + // 黄哥写于2014年3月19日北京 + + import ( + "fmt" + "math/rand" + ) + + // 下面这个函数判断一个一维slice在不在二维slice中,相当于python中in功能 + func exist_in(str1 [][]string, str2 []string) int { + for _, item := range str1 { + if item[0] == str2[0] && item[1] == str2[1] { + return 1 + } + } + return 0 + + } + func main() { + var person string + guess_list := []string{"石头", "剪刀", "布"} + Win := [][]string{{"布", "石头"}, {"石头", "剪刀"}, {"剪刀", "布"}} + + for { + num := rand.Intn(len(guess_list)) + computer := guess_list[num] + fmt.Println(computer) + fmt.Println("请输入'石头,剪刀,布'") + fmt.Scanf("%s\n", &person) + input := []string{computer, person} //构造一个一维slice + if computer == person { + fmt.Println("平手!") + + } else if exist_in(Win, input) > 0 { + fmt.Println("电脑获胜") + + } else { + fmt.Println("人获胜") + break + } + } + + } \ No newline at end of file