轮盘赌程序把现金加起来

2024-09-30 22:27:31 发布

您现在位置:Python中文网/ 问答频道 /正文

Python 3.5

我有一个项目,为一个班创建一个轮盘赌轮迷你游戏,我有问题。我将初始现金设置为100美元,让用户玩轮盘赌。在他们下注后,到了为下一轮计算现金的时候,我在设置新的现金价值时遇到了问题。基本上,我需要把赢/亏加在现金的价值上,这样它就可以准确地更新到下一轮了。我知道把现金作为一个全局变量是错误的,但是我们还没有学会正确的方法来做,也没有时间亲自去检查。不管怎样,这个问题已经接近底部了。谢谢你的帮助!-在

import math
import random

def main():
    global cash

    print('Welcome to Roulette! We\'ll start you with $100')

    cash = 100 #set to 100 for intitial

    menu()

def menu():

    print('Place your bet! ',cash,'bucks!', '''
=======================================
1. Bet on Red (pays 1:1)
2. Bet on Black (pays 1:1)
3. First 12 (pays 2:1)
4. Middle 12 (pays 2:1)
5. Last 12 (pays 2:1)
6. Choose any number (pays 35:1)
7. Cash out
Please enter your choice: ''')
    menuChoice = int(input())
    #Add validation!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    if cash > 0 and menuChoice != 7: #Determine if quit or broke
        if menuChoice == 6:
            number = int(input('Please choose a number from 0-36!')) #Get their specific number
            while number < 0 or number > 36: #Validation
                number = int(input('Please enter a number from 0-36'))


        wager = int(input('How much would you like to bet? '))
        #Add validation!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        print('Press any key to spin the wheel! ')
        input()
        print(menuChoice, wager)
##
##        ball = random.randint(0,36)
        ball = 19 #set to 19 for testing. REMOVE AND RESET BALL!!!!!!!!!!!!!!!!!

        if ball == 0:
            color = ('green')
        elif ball % 2 == 0:
            color = ('black')
        else:
            color = ('red')

        print('Your ball was',ball, 'and landed on the color',color)

        #Determine if winner
        if menuChoice == 1 and color == 'red':
            winner = True
            odds = 1

        elif menuChoice == 2 and color == 'black':
            winner = True
            odds = 2

        elif menuChoice == 3 and ball >= 1 and ball <= 12 :
            winner = True
            odds = 2

        elif menuChoice == 4 and ball >= 13 and ball  <= 24:
            winner = True
            odds = 2

        elif menuChoice == 5 and ball >= 25 and ball  <= 36:
            winner = True
            odds = 2

        elif menuChoice == 6 and ball == number:
            winner = True
            odds = 35

        else:
            winner = False
            odds = 0

            #End determine if winner

        if odds == 0:
            pass
        else:
            amount = wager * odds   #Get amount won/lost
            print(amount)

        if winner == True:
            cash += amount #<~~~~~~~~~~~~~Problem Area
            print('Congratulations! You won', wager,'dollars!')
            print('Your total is now :',cash,'dollars.')
        else:
            cash -= wager
            print('Sorry! You lost',wager,'dollars. Better luck next time!')
            print('Your total is now :',cash,'dollars.')

        input('Press a key to go back to the menu!')

        print('====================================================================')

        #New round
        menu()




    else:
        print('Thank you for playing! ')
        exit

main()

Tags: andtotruenumberinputifcashcolor
1条回答
网友
1楼 · 发布于 2024-09-30 22:27:31

您可以使用已有的方法创建自己的python类。然后可以用参数^{}声明{}一个类变量。使用self.cash可以访问每个方法中的变量。如果这没有帮助,请用你的问题评论这个答案。在

相关问题 更多 >