|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
写了一个比大小,但是结果总输,求助!!!!
import random
def roll_dice(numbers=3,points=None):
print('<<<<< 摇色子!<<<<<')
if points is None:
points = []
while numbers > 0:
point = random.randrange(1,7)
points.append(point)
numbers = numbers - 1
return points
def roll_result(total):
isbig = 11 <= total <=18
issmall = 3 <= total <=10
if isbig:
return "大!"
elif issmall:
return '小!'
def start_game():
your_money=1000
print('你有1000筹码,筹码为0时,游戏结束')
while your_money >0:
print('<<<<< GAME STARTS!<<<<<')
choices = ['大','小']
your_choice = input('大或小:')
if your_choice in choices:
your_bet = int(input('你想下多少注?'))
your_rate= int(input('请输入赔率?'))
points = roll_dice()
total = sum(points)
youwin = your_choice == roll_result(total)
if youwin:
print('点数是:',points, total, roll_result(total), '你赢了!')
print('你赢了{},你还有{}'.format(your_bet*your_rate,your_money + your_bet*your_rate))
your_money = your_money + your_bet*your_rate
else:
print('点数是:',points, total, roll_result(total), '你输了!')
print('你输了{},你还有{}'.format(your_bet*your_rate,your_money - your_bet*your_rate))
your_money = your_money - your_bet*your_rate
else:
print('必须输入大或小')
start_game()
else:
print('GAME OVER!')
start_game()
因为 youwin = your_choice == roll_result(total)里的roll_result(total)的返回值是(大!)或者(小!)而your_choice的输入值是(大)或者(小)所以your_choice永远不会=roll_result(total),也就是说youwin永远是是假,所以永远不会执行if youwin:的真值,而是执行else里边的语句了,这就是总是输的原因。像前边几个大能说的把 return "大!"return '小!'里边的!去掉是最好的改法。
另外
else:
print('必须输入大或小')
start_game()
这句貌似有问题,如果我不输入大或者小,它告诉我必须输入大小,然后调用start_game()函数,这样也导致了就是我最后输光了,游戏也不会结束,接着让我输入大小,直到我调用的start_ganme()函数次数结束,才能结束,也就是说我输入错几次,他的额外调用了几次start_ganme()函数,我就得输光了也的多玩几次才能结束游戏。所以你可以吧直接把start_game()这句去掉,里边流程 print('<<<<< GAME STARTS!<<<<<'),如果你感觉不满意可以调节一下,调到your_money=1000后。
|
|