2013-03-30 93 views
0

我正在嘗試以最小,最大和特定的增量請求特定的投注額。當我運行下面的代碼時,程序會遍歷數組中的每個元素而不是停止。if python命令 - 輪盤賭模擬器

from pylab import * 
import random 
bank = 20000 
betrange=range(100,20100,100)  
print "Hello Kathy, welcome to the Golden Brighton casino. We have reserved a high roller table for your bank of 20,000. How much would you like to bet per spin? The table has a 100 minimum bet with a maximum of 20,000." 
print "The available legal bets are as follows:" 
bet=raw_input("Place your bets please:") 
for i in range(len(betrange)): 
    if betrange[i] == bet: 
     print bet,"Your bet has been accepted, can you make a million?" 
     break 
    else: 
     print bet,"Please enter a legal bet. The table minimum is 100, with a maximum of 20000 in increments of 100." 

回答

3

它看起來像你的代碼會變得更好的比一個循環的表達式:

if 100 <= bet <= 20000 and bet%100 == 0: 
    print bet,"Your bet has been accepted" 
else: 
    print bet,"Please enter a valid bet" 
+0

嘛,我很老(BBC BASIC)輪盤模擬器,我只是用一個GOTO,但出於恐懼猛禽攻擊(http://xkcd.com/292/),我會建議你在'while 1'循環中附加'bet = raw_input(...)'並且附加條件,然後添加一個「打破」到「接受」的一面。 –

+0

謝謝你的幫助,像魅力一樣工作。 –