一、导入库
import random
import time
二、注册用户
我们用变量与input实现
name = str(input('请输入用户名:'))
print('欢迎您,'+name)
三、注册年龄
这里我们得用except制作乱输文本就游戏结束的程序
乱输文本就结束
try:
age = int(input('请输入年龄:'))
except ValueError:
print('非法输入')
age = 30000
顺便把年龄设为30000[滑稽]
再根据年龄大小分配金币
四、分配金币
if age <10:
gold = 500
print('您的初始金币为500')
if age <20 and age >10:
gold = 1000
print('您的初始金币为1000')
if age >20 and age <30:
gold = 1500
print('您的初始金币为1500')
if age >30 and age <1000:
gold = 200
print('年龄过大,您的初始金币为200')
用if语句保证金币小于10000,如果等于10000,游戏结束
五、if判断 1
if gold !=10000:
Game_start = str(input('是否开始游戏?(True或False)'))
while gold !=10000:
接下来就是最重要的游戏部分了,认真!
我们还得用if语句判断Game_start变量为True,并创建一个随机数列表
六、if与随机数
if Game_start =='True':
list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
while list_123 ==10:
list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
然后设定回答变量和正确答案与错误答案
七、回答、正确与错误
answer = str(input('请猜数(big或small):'))
if list_123[0] + list_123[1] + list_123[2] >10:
result = 'big'
error_result = 'small'
if list_123[0] + list_123[1] + list_123[2] <10:
result = 'small'
error_result = 'big'
还得用if语句判断正确、错误与非法输入的答案
八、if判断 2
if answer ==result:
print('您赢了!')
gold = gold + 100
print('随机数为'+str(list_123))
print('您现在有'+str(gold)+'金币')
elif answer ==error_result:
print('您输了...')
print('随机数为'+str(list_123))
gold = gold - 100
print('您现在有'+str(gold)+'金币')
else:
print('非法输入')
gold = 10000
再制作购买道具的模块
太长了我懒得写一句一句解析
九、购买道具
if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
answer = str(input('您现在可以购买道具,是否购买?(True或False):'))
if answer =='True':
print('请说出要购买der道具')
print('*'*41)
print('*金币翻倍器[现有的] 金币翻倍器[得到的]*')
print('* 2000G 1500G *')
print('*'*41)
answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):'))
if gold >2000 and answer =='1':
gold = gold - 2000
answer = int(input('请说出要翻der倍数:'))
gold = str(gold * answer)
print('您现在有'+gold+'金币')
gold = int(gold)
elif gold >1500 and answer =='2':
gold = gold - 1500
answer = int(input('请说出要翻der倍数:'))
gold_mang = 100 * answer
gold = gold + gold_mang
print('您现在有'+str(gold)+'金币')
gold = int(gold)
else:
print('没有这个道具,游戏结束')
gold = 10000
elif answer =='False':
print('继续游戏')
else:
print('非法输入')
gold = 10000
我写到一半发现没有把上面的桥接上,所以现在来桥接
十、桥接
import random
import time
name = str(input('请输入用户名:'))
print('欢迎您,'+name)
try:
age = int(input('请输入年龄:'))
except ValueError:
print('非法输入')
age = 30000
gold = 10000
if age <10:
gold = 500
print('您的初始金币为500')
if age <20 and age >10:
gold = 1000
print('您的初始金币为1000')
if age >20 and age <30:
gold = 1500
print('您的初始金币为1500')
if age >30 and age <1000:
gold = 200
print('年龄过大,您的初始金币为200')
if gold !=10000:
Game_start = str(input('是否开始游戏?(True或False)'))
while gold !=10000:
if Game_start =='True':
list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
while list_123 ==10:
list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
answer = str(input('请猜数(big或small):'))
if list_123[0] + list_123[1] + list_123[2] >10:
result = 'big'
error_result = 'small'
if list_123[0] + list_123[1] + list_123[2] <10:
result = 'small'
error_result = 'big'
if answer ==result:
print('您赢了!')
gold = gold + 100
print('随机数为'+str(list_123))
print('您现在有'+str(gold)+'金币')
elif answer ==error_result:
print('您输了...')
print('随机数为'+str(list_123))
gold = gold - 100
print('您现在有'+str(gold)+'金币')
else:
print('非法输入')
gold = 10000
if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
answer = str(input('您现在可以购买道具,是否购买?(True或False):'))
if answer =='True':
print('请说出要购买der道具')
print('*'*41)
print('*金币翻倍器[现有的] 金币翻倍器[得到的]*')
print('* 2000G 1500G *')
print('*'*41)
answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):'))
if gold >2000 and answer =='1':
gold = gold - 2000
answer = int(input('请说出要翻der倍数:'))
gold = str(gold * answer)
print('您现在有'+gold+'金币')
gold = int(gold)
elif gold >1500 and answer =='2':
gold = gold - 1500
answer = int(input('请说出要翻der倍数:'))
gold_mang = 100 * answer
gold = gold + gold_mang
print('您现在有'+str(gold)+'金币')
gold = int(gold)
else:
print('没有这个道具,游戏结束')
gold = 10000
elif answer =='False':
print('继续游戏')
else:
print('非法输入')
gold = 10000
其实到这里我们就基本完成了,但如果只写到这里会有很多bug,不想看了也可以就玩这个版本
还剩了一些代码,我也懒得写了,直接完整代码
十一、完整代码
import random
import time
name = str(input('请输入用户名:'))
print('欢迎您,'+name)
try:
age = int(input('请输入年龄:'))
except ValueError:
print('非法输入')
age = 30000
gold = 10000
if age <10:
gold = 500
print('您的初始金币为500')
if age <20 and age >10:
gold = 1000
print('您的初始金币为1000')
if age >20 and age <30:
gold = 1500
print('您的初始金币为1500')
if age >30 and age <1000:
gold = 200
print('年龄过大,您的初始金币为200')
if gold !=10000:
Game_start = str(input('是否开始游戏?(True或False)'))
while gold !=10000:
if Game_start =='True':
list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
while list_123 ==10:
list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
answer = str(input('请猜数(big或small):'))
if list_123[0] + list_123[1] + list_123[2] >10:
result = 'big'
error_result = 'small'
if list_123[0] + list_123[1] + list_123[2] <10:
result = 'small'
error_result = 'big'
if answer ==result:
print('您赢了!')
gold = gold + 100
print('随机数为'+str(list_123))
print('您现在有'+str(gold)+'金币')
elif answer ==error_result:
print('您输了...')
print('随机数为'+str(list_123))
gold = gold - 100
print('您现在有'+str(gold)+'金币')
else:
print('非法输入')
gold = 10000
if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
answer = str(input('您现在可以购买道具,是否购买?(True或False):'))
if answer =='True':
print('请说出要购买der道具')
print('*'*41)
print('*金币翻倍器[现有的] 金币翻倍器[得到的]*')
print('* 2000G 1500G *')
print('*'*41)
answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):'))
if gold >2000 and answer =='1':
gold = gold - 2000
answer = int(input('请说出要翻der倍数:'))
gold = str(gold * answer)
print('您现在有'+gold+'金币')
gold = int(gold)
elif gold >1500 and answer =='2':
gold = gold - 1500
answer = int(input('请说出要翻der倍数:'))
gold_mang = 100 * answer
gold = gold + gold_mang
print('您现在有'+str(gold)+'金币')
gold = int(gold)
else:
print('没有这个道具,游戏结束')
gold = 10000
elif answer =='False':
print('继续游戏')
else:
print('非法输入')
gold = 10000
if gold ==0:
print('金币没了,游戏结束')
gold = 10000
elif Game_start =='False':
print('欢迎游玩,下次再见!')
gold = 10000
else:
print('非法输入')
gold = 10000
print('游戏结束,再见!')
time.sleep(10)
到此这篇关于python实战之90行代码写个猜数字的文章就介绍到这了,更多相关python猜数字内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!