python猜数小游戏

import random

i = random.randint(0, 20)
print('-' * 30)
ran = input('是否开始游戏(y/n):')
n = 0
while True:
n += 1
if ran == 'y':
san = input('请猜一个0-20之间的数:')
# san是字符串,i并不是,需要将i改为字符串
if san == str(i):
print('恭喜你猜对了!')
print('共猜了',n,'局')
if n == 1:
print('运气爆棚!赶紧去买彩票吧!')
break
elif n > 6:
print('运气不太好啊!')
break
elif n >= 2:
print('还不错,再接再厉!')
break
elif san >= str(i):
print('猜大了再小一点!')
elif san <= str(i):
print('猜小了再大一点!')
elif ran != 'y' and ran != 'n':
print('没有此选项!')
break
elif ran == 'n':
print('退出成功!')
break