简体   繁体   English

Python中的基本5选择轮盘游戏

[英]basic 5 choice roulette game in python

new python guy here 新的Python家伙在这里

so i am trying to make a basic roulette game with 5 choices for the player to choose from. 因此,我尝试制作一款具有5种选择的基本轮盘游戏,供玩家选择。 i managed to get the script to run without errors, but the section under "win_loss" doesn't seem to be registering, or at least not the if/elif parts. 我设法使脚本运行没有错误,但是“ win_loss”下的部分似乎没有注册,或者至少没有if / elif部分。 i can only assume that i didn't identify something correctly at the top. 我只能假设我没有正确识别顶部的内容。

tho i would gladly accept a finished answer, guidance would probably help me lean more. 我很乐意接受一个完整的答案,指导可能会帮助我更多地学习。 either way, all help is welcome. 无论哪种方式,欢迎所有帮助。

import random

red = (1,3,5,7,9,12,14,16,18,21,23,25,27,30,32,34,36)
black = (2,4,6,8,10,11,13,15,17,19,20,22,24,26,28,29,31,33,35)
green = 0
even = (2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36)
odd = (1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35)

def main():
    print('Red = 1')
    print('Black = 2')
    print('Green = 3')
    print('Odd = 4')
    print('Even = 5')
    print('-=-=-=-=-=-=-=-=-=-=-')
    player = int(input('Place your bet, 1-5: '))
    print('-=-=-=-=-=-=-=-=-=-=-')

    roll()
    win_loss()

def roll():
    spin = random.randint(1,36)
    print('Landed on: ',spin)
    print('-=-=-=-=-=-=-=-=-=-=-')

def win_loss():
    if (roll) == red:
        print('You won $.45')
    elif (roll) == black:
        print('You won $.45')
    elif (roll) == green:
        print('You won $5.00')
    elif (roll) == even:
        print('You won $.45')
    elif (roll) == odd:
        print('You won $.45')
    else:
        print('You lost')

main()

Your function don't return anything and they don't accept parameters. 您的函数不返回任何内容,也不接受参数。 To have your win_loss() function know what the result of the roll was you need to do somehting like this: 要使win_loss()函数知道滚动结果是什么,您需要执行以下操作:

def roll():
    return random.randint(0,36)  # Pointed out by Lallen, you need to include 0
    # move the print statements outside the function

def win_loss(roll):  # note the parameter here
    if (roll) == red:
        print('You won $.45')
    elif (roll) == black:
        print('You won $.45')
    elif (roll) == green:
        print('You won $5.00')
    elif (roll) == even:
        print('You won $.45')
    elif (roll) == odd:
        print('You won $.45')
    else:
        print('You lost')

And in your script: 在您的脚本中:

result = roll()
win_loss(result)

Read more about functions and parameters here. 在此处阅读有关功能和参数的更多信息。

enter code here

Also note that your Boolean operations will not work. 另请注意,布尔运算将不起作用。 You need to test if the result of the roll is in your tuples not equal to them. 您需要测试滚动结果是否in元组中不等于它们。 Something like this: 像这样:

# Note the curly braces. As Marius suggested using sets here will make your program a little more efficient
red = {1,3,5,7,9,12,14,16,18,21,23,25,27,30,32,34,36}
black = {2,4,6,8,10,11,13,15,17,19,20,22,24,26,28,29,31,33,35}
green = 0
even = {2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36}
odd = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35}

...

def win_loss(roll):  # note the parameter here
    if roll in red:  
        print('You won $.45')

This will work much better now as 1 in red will be True while 1 == red will be False . 现在效果会更好,因为1 in red将为True1 == red将为False

You will also need to make sure the players bet was a winning/losing one: 您还需要确保玩家的赌注是赢/输:

def win_loss(roll, player):  # note the new player parameter here
        if roll in red and player == 1:  # note the comparison between the players input and the choices you laid out in the beginning.
            print('You won $.45')

That extra Boolean expression will make sure the roll was in red and that the player bet on red. 该额外的布尔表达式将确保掷骰为red ,并且玩家押注为红色。 This will have to be done for all the different checks. 对于所有不同的检查,都必须这样做。

These as well as the points listed in the comments are some things that will help you along. 这些以及注释中列出的要点将对您有所帮助。 I would strongly suggest doing a bit more reading or perhaps some online tutorials as there are a lot of potential problems in your code. 我强烈建议您多做一些阅读,或者也许做一些在线教程,因为您的代码中存在很多潜在的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM