轮盘计划-加现金

时间:2018-10-16 04:46:34

标签: python python-3.x

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()

1 个答案:

答案 0 :(得分:1)

您可以使用已经拥有的方法创建自己的python类。比起可以用参数self声明cash一个类变量。使用self.cash可以在每种方法中访问变量。如果这样做没有帮助,请在您的问题中对此答案发表评论。