用python IDE设计第一个小游戏
猜一下我现在想的数字一个数字
规则:①你一共有三次机会,直到才对为止
②1-10之间的整数
"""用python设计第一个游戏"""
import random
count = 3
answer = random.randint(1,10)
while count > 0:
temp = input("猜一下我现在想的数字一个数字:")
guess = int(temp)
if guess == answer:
print("恭喜您,对了")
print("可惜了,对了也没奖励")
break
else:
if guess < 8:
print("猜小辣")
else:
print("猜大辣")
count = count - 1
print("游戏结束")
涉及到的知识点:
- 导入随机函数 import random
- 循环结构 while
- input 输入函数,用于接受一个标准输入数据,且返回string类型
- if else 条件语句+嵌套使用
新手易错点:
- 未导入随机函数
- 代码中出现中文符号
- 缩进错误,python代码非常注重缩进,同样长度的缩进表示一个代码块(在if语句中,缩进通常某行代码是否执行)