Python初学者随笔(一)_ 用Python写的第一个游戏“猜数字”

如标题所写,这篇随笔主要记录下学习Python过程中用Python写的第一个游戏——“猜数字”_跟着“小甲鱼”学Python,链接: https://b23.tv/BV1c4411e77t

 1 # -*- coding: cp936 -*-
 2 """用Python设计第一个游戏"""
 3 import random
 4 count = 3
 5 answer = random.randint(1,10)
 6 while count > 0:
 7     temp = input("请输入一个数字:")
 8     guess = int(temp)
 9     if guess == answer:
10         print("猜对了")
11         break
12     else:
13         if guess < answer:
14             print("小啦")
15         else:
16             print("大啦")
17     count = count - 1
18 print("游戏结束")

至此,第一个使用Python写的程序代码结束。

---------------------------------------------------------------

下面是一些笔记

首先,最重要的是Python要求严格的对齐与缩进。用以区分语句的块

其次第3行

import random

是使用

import

插入“random”模块,“random”模块的作用是产生(伪)随机数

语法为:

import random
random.randint(1,10) #(1,10)是产生随机数的范围

记录于200706

 

posted @ 2020-07-06 19:05  Yamz  阅读(154)  评论(0编辑  收藏  举报