python猜大小游戏_python做一个摇骰子猜大小游戏,请问我的代码哪儿不对?

2018-11-26 回答

最后一个条件有问题,应该是除了7,11,2,3,12外的数字会继续摇。

#!/usr/bin/env python

#coding: utf-8

import sys

from random import seed, randint

from time import sleep

# make random seed

seed()

dashs = '-' * 50

# loop to get player chips

while true:

wallet = str(raw_input("how many chips do you want to buy? "))

if not wallet.isdigit():

print "***error: chips must be positive number!***"

continue

wallet = int(wallet)

break

def continue_or_not():

while true:

answer = str(raw_input("do you want to continue (y/n) [y]?"))

answer = 'y' if answer == '' else answer

if answer.upper() not in ('y', 'n'):

print '***error: wrong answer!***'

continue

return answer.upper()

def roll_dice():

first, second = randint(1, 6), randint(1, 6)

total = first + second

print 'you rolled %d + %d = %d' % (first, second, total)

return total

# game begin

while true:

print dashs

print "there're %d chips in your wallet." % wallet

bet = str(raw_input("please make a bet: "))

if not bet.isdigit():

print "***error: chips must be positive number!***"

continue

bet = int(bet)

if bet > wallet:

print "***error: only %d chips in your wallet!***" % wallet

continue

if bet < 1:

print "***error: chips must be positive number!***"

continue

total = roll_dice()

if total in (7, 11):

game_status = 'won'

elif total in (2, 3, 12):

game_status = 'lost'

else:

game_status = 'continue'

my_point = total

while game_status == 'continue':

sum = roll_dice()

if sum == my_point:

game_status = 'won'

elif sum == 7:

game_status = 'lost'

if game_status == 'won':

print 'congratulations, you won!'

wallet += bet

else:

wallet -= bet

print 'sorry, you lost.'

if wallet < 1:

print 'you are broken.'

break

print dashs

answer = continue_or_not()

if answer == 'y':

continue

else:

break

# player want to quit

print dashs

print 'there are %d chips in your wallet.' % wallet

time_to_left = 5

while time_to_left > 0:

print '\\rquit in %d seconds ...' % time_to_left,

time_to_left -= 1

sleep(1)

sys.exit(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值