该程序从初始余额开始,个人账面余额必须与初始余额匹配才能继续进行。然后,用户输入当天的选择以及赌注、赔率等,然后程序用新的账簿余额计算新的余额。
def main():
def balances(msg):
while True:
try:
x = float(raw_input(msg))
return x
except ValueError:
print"That's not a number"
continue
while True:
balance = balances('Balance:')
print
bfair_balance = balances('bfair:')
wh_balance = balances('wh:')
freds_balance = balances('freds:')
sky_balance = balances('sky:')
pp_balance = balances('pp:')
balance_sum = pp_balance + bfair_balance + sky_balance + freds_balance + wh_balance
if balance == balance_sum:
# balance is correct -> stop the loop
break
else:
print "Balances do not match"
print
print "Balance: %s" %balance
print "Bfair: %d, Sky: %d, pp: %d, freds: %d, wh: %d" %(bfair_balance, sky_balance, pp_balance, freds_balance, wh_balance)
books = [bfair_balance, wh_balance, sky_balance,freds_balance,pp_balance]
#print books
print
inputs = []
new_books = []
def looks_good(inputs):
for i in inputs:
print i
while True:
add_selection =raw_input("Would you like to add a selection? ")
if add_selection == "Yes":
print
selection = raw_input('Horse: ')
stake = float(raw_input('Stake: '))
while stake <=0:
print "Please enter a stake greater than 0"
stake = float(raw_input('Stake: '))
while stake > bfair_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
while stake > pp_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
while stake > freds_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
while stake > sky_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
while stake > wh_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
odds = float(raw_input('Odds: '))
while odds <=0:
print "Please enter odds greater than 0"
odds = float(raw_input('Odds: '))
result = (raw_input('Result: '))
if result == "Win":
result = stake * odds
print "Returns:%d"%result
elif result == "Lose":
result = 0 * odds
print "Returns:%d"%result
book = raw_input('Book: ')
while book not in['bfair','sky','wh','freds','pp']:
print "That's not valid"
book = raw_input('Book: ')
if result == 0 and book == "bfair":
bfair_balance = bfair_balance - stake
new_books.append(bfair_balance)
elif result == (stake * odds) and book == "bfair":
bfair_balance = bfair_balance - stake + (stake * odds)
new_books.append(bfair_balance)
if result == 0 and book == "freds":
freds_balance = freds_balance - stake
new_books.append(freds_balance)
elif result == (stake * odds) and book == "freds":
freds_balance = freds_balance - stake + (stake * odds)
new_books.append(freds_balance)
if result == 0 and book == "pp":
pp_balance = pp_balance - stake
new_books.append(pp_balance)
elif result == (stake * odds) and book == "pp":
pp_balance = pp_balance - stake + (stake * odds)
new_books.append(pp_balance)
if result == 0 and book == "wh":
wh_balance = wh_balance - stake
print wh_balance - stake
new_books.append(wh_balance)
elif result == (stake * odds) and book == "wh":
wh_balance = wh_balance - stake + (stake * odds)
new_books.append(wh_balance)
if result == 0 and book == "sky":
sky_balance = sky_balance - stake
new_books.append(sky_balance)
elif result == (stake * odds) and book == "sky":
sky_balance = sky_balance - stake + (stake * odds)
new_books.append(sky_balance)
my_list=[selection,stake,odds,result,book]
inputs.append(my_list)
print
total_stake=[]
for my_list in inputs:
total_stake.append(my_list[1])
print "Total Stake: %d" %sum(total_stake)
total_winnings = []
for my_list in inputs:
total_winnings.append(my_list[3])
print "Total Winnings: %d" %sum(total_winnings)
print
new_balance = balance - sum(total_stake) + sum(total_winnings)
print "New Balance:%d" %new_balance
print "Bfair: %d, Sky: %d, pp: %d, freds: %d, wh: %d" %(bfair_balance, sky_balance, pp_balance, freds_balance, wh_balance)
print
elif add_selection == "No":
break
looks_good(inputs)
import os
os.system("pause")
if __name__ == '__main__':
main()发布于 2016-08-08 11:00:35
例如,您有:
while stake > bfair_balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))
# many very similar blocks您应该使用循环来避免重复:
for balance in [bfair_balance, ...]:
while stake > balance:
print "You do not have sufficient funds"
stake = float(raw_input('Stake: '))https://codereview.stackexchange.com/questions/138083
复制相似问题