---
title: "python基础--循环--模拟骰子"
author: "guhao022"
date: "2014-08-07T11:39:13Z"
tags: ["IDE", "Python", "游戏"]
categories: ["编程语言"]
slug: "python"
draft: false
lastmod: "2014-08-07T11:39:13Z"
id: 2101264
url: "https://guhao022.iteye.com/blog/2101264"
---

IDE选择

python测试只需要用python shell就可以了，但是写程序还是需要一个IDE的，喜欢全面一点的可以选择pycharm，不过这个要付费，不过在天朝，你懂得，呵呵。喜欢轻便一点的可以选择神灯，Geany,跨平台，win、mac和linux都可以用，可以试试，很爽，支持很多语言，c++、c、php、python.....关于Genay的配置去找一下我的一篇文章[《Geany 跨平台编辑器》](http://golune.com/blog/content/ce145a59fa624c9afdc24e207e166e491a364)

 

怎么安装python就不重复了，网上很多，从循环开始

先来用python做一个模拟骰子的小游戏，非常简单

编辑器可以用Geany或者官方IDLE，但是初学不推荐大型的IDE，好用归好用，但是个人觉得太大写个小程序不怎么方便，不过随意，推荐Geany

for循环

```python
__author__ = 'guhao'
#模拟骰子游戏
import random  #引入库文件
for x in range(1,11):    #循环从1到11，不包括11（循环10次）
    #以 ： 结尾表示代码还没有结束  而且冒号以后的代码注意缩进，缩进不一致会导致代码错误
    num = random.randint(1,6) #随机输出1到6任意数字
    print(num)  #输出num
```

把上面代码copy到一个扩展名为py的文件中，执行一下

```
$ python3 1.py
3
2
2
4
1
5
6
1
2
1
```

可以多执行几次试试，好了，最基本的模拟骰子就成功了，给这个小游戏加点料，试试 if 语句

 

if语句

开始

```python
import random
for x in range(1, 11):
    throw1 = random.randint(1, 6)    #投掷一次
    throw2 = random.randint(1, 6)    #投掷第二次
    total = throw1+throw2 #两次投掷相加
    print(total)
    if total == 2:       #if语句，如果两次投掷的和total等于2
        print('两次投掷结果为2')
        #结束if循环
    if total == 5:  
        print('两次投掷结果为5')
    if total == 10:
        print('两次投掷结果为10')
    if throw1 == throw2:
        print('太厉害了，Double Thrown!')
```

多执行这个文件，挺有趣的

 

逻辑计算

True和False叫做逻辑值，if后面的条件只有返回True的时候才会执行，比如投掷骰子游戏，让结果只显示6-12的数值

```python
if total >=6 and total <=12:    #投掷的和小于3或者大于8，两个都成立才会返回True，执行下面的代码
    print(total)
```

这里用的是and ；and叫做逻辑运算符，逻辑运算符有三个 and、or和not； 比如用or来修改一下游戏

```python
if total < 3 or total > 8:    #投掷的和小于3或者大于8，两者只要有一个成立就返回True，执行下面的代码
    print(total)
```

下面是not

```python
if not(total < 6 ):    #投掷的和小于6时返回不执行下面的代码，大于等于6时才返回True，执行下面的代码
    print(total)
```

else语句

忘了else语句，else时和if同时使用的，当if语句返回False执行else语句，在shell里演示一下：

```javascript
>>>a = 6
>>> if a > 6:
...   print('a is big')
... else:
...   print('a is small')
... 
a is small
```

 

while语句

while循环和for循环有些不同，while循环后面跟一个条件，当条件为真（True）时就会一直循环下去，所以使用while循环的时候一定要考虑清楚，如果你写了一个条件永远为真的while循环，呵呵......

我们可以用while来修改一下模拟骰子游戏，让骰子一直丢，直到两次丢的骰子都为6时才停止，代码如下：

```python
import random
throw1 = random.randint(1, 6)    #投掷一次
throw2 = random.randint(1, 6)    #投掷第二次
while not(throw1 == throw2 == 6):  #如果两次投掷的结果都不为6，执行while里面的代码
    total = throw1 + throw2
    print(total)
    #继续投掷，直到两个都等于6时停止
    throw1 = random.randint(1, 6)
    throw2 = random.randint(1, 6)
print('两次投掷结果都为6')
```

上面一段代码可以正常运行，但是有些臃肿，因为有两段代码时重复的，当然几句代码的小程序无所谓，但是到了写一些比较大点的程序的时候这样肯定时不可取的，所以可以优化一下

```python
import random
while True:    #当为真的时候，里面的程序会一直循环下去，用break跳出
    throw1 = random.randint(1, 6)
    throw2 = random.randint(1, 6)
    total = throw1 + throw2
    print(total)
    if throw1 == throw2 == 6:
        break   #跳出循环  
print('两次投掷结果都为6')
```

这样就简单多了......

程序界一条“军规” ———DRY（Don't Repeat Yourself，拒绝重复）

 

> 注：如果你选择copy上面的代码，一定要记住python里面缩进不一致会导致程序出错，所以，还是自己敲吧！

 

 

`发布于：`[http://www.golune.com/blog/content/83a955a87beebe7458583b72fcc39282242ef](http://www.golune.com/blog/content/83a955a87beebe7458583b72fcc39282242ef)