2016-10-02 29 views
1
#This is a roulette wheel 

import random 
import time 
#Randomiser 
green = [0] 
red = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35] 
black = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36] 
spin = random.randint (0,36) 
#Program 
bet = int(input("Enter your Bets: ")) 
colour = input("Pick your Colour: ") 
print ("The wheel is spinning") 

if spin in green: 
    print ("The ball stopped on green") 
    print ("You won",bet*100,"!") 

if spin in red: 
    print ("The ball stopped on red") 
    print ("You won",bet*2,"!") 

if spin in black: 
    print ("The ball stopped on black") 
    print ("You won",bet*2,"!") 

它告訴是否有人贏了。但它不能分辨是否有人丟失。我一直在學習python幾個月,並想知道是否有人可以幫助我。謝謝!贏錢和丟輪盤

回答

-1
if spin in green: 
    winning_color = "green" 
    print("The ball landed on", winning_color) 
if spin in red: 
    winning_color = "red" 
    print("The ball landed on", winning_color) 
if spin in black: 
    winning_color = "black" 
    print("The ball landed on", winning_color) 

if winning_color == colour: 
    print("Congrats you won!") 
    print("You won", bet*2, "!") 

else: 
    print("Sorry you lose") 

一個需要注意的,你應該使用

if somethingHappens: 
    doSomething 
elif somethingElseHappens: #Stands for else if, must be after the first if 
    doThisInstead 
else: #This is Pythons catchall if the conditions above fail, this will run 
    doThisIfNothingElse 
+0

哈哈我的第一個答案,我搞砸了。 – bobbyanne

+0

謝謝你解釋循環。這是我在課堂上不了解的一件事。謝謝! – ItsTate

0

也許我不知道輪盤賭規則,但好像你已經編寫它總是打印「你贏了恭喜!」

首先,你需要給玩家選擇(顏色)分配給可測試值,這對我們來說是列出了設置:

if "red" in colour: 
    colour = red 

你顯然會在這裏添加其他顏色。

下一個需要考驗玩家的選擇對自旋值,這樣的事情會的工作,你會明顯需要修改它雖然適合您的方案:

if spin in colour: 
    print("you win") 
else: 
    print("you lose") 

然後根據需要修改

勝果
0

看起來像是0,奇數是紅色,偶數是綠色。所以這會更好的爲你:

import random 

bet = int(input("Enter your Bets: ")) 
colour = input("Pick your Colour: ") 
print ("The wheel is spinning") 

spin = random.randrange(0,37) 

if spin == 0: 
    print ("The ball stopped on green") 
    print ("You won",bet*100,"!") 

print ("The ball stopped on", ['black', 'red'][spin%2]) 
elif ['black', 'red'][spin%2] == color: 
    print ("You won",bet*2,"!") 
else: 
    print ("You lost!")