简单的轮盘模拟器,赌注始终为奇数,但它是无限的

问题描述 投票:0回答:1

尝试创建一个只押奇数的轮盘模拟器。这是一个示例执行:

Profit Goal: 1500
Starting money: 1000
Table minimum bet: 25
Table maximum bet: 300
Spin: 16. You lost $25 on the bet. You had 100. Now you have 75 left.
Spin: 22. You lost $50 on the bet. You had 75. Now you have 25 left.
Spin: 33. You won $25 on the bet. You had 25. Now you have 50 left.
Spin: 1. You won $25 on the bet. You had 50. Now you have 75 left.
Spin: 29. You won $25 on the bet. You had 75. Now you have 100 left.
Spin: 15. You won $25 on the bet. You had 100. Now you have 125 left.
Spin: 3. You won $25 on the bet. You had 125. Now you have 150 left.
You reached your goal and beat the casino. You should stop now. You have $150.

我的代码

Profit_Goal = int(input('Profit Goal:'))
Starting_Money = int(input('Starting money:'))
Table_Min = int(input('Table minium bet:'))
Table_Max = int(input('Table maximum bet:'))
Bet = Table_Min
Money = Starting_Money
while 0 < Money <= Profit_Goal:
    number = random.randint(1, 36)
    print('Spin:', number)
    if number % 2 != 0:
        Money = Money + Bet
        print('You won', Bet, 'on the bet. You had', Money, 'Now you have', Bet + Money)
    elif number % 2 == 0:
        Money = Money - Bet
        Bet = Bet * 2
        print('You lost', Bet, 'on the bet. You had', Money, 'Now you have', Money - Bet)
    elif Money >= Profit_Goal:
        print('You reached your goal and beat the casino. You should stop now. You have', Money)
    else:
        print('You have lost all of your money')

代码不停地循环,但我不知道为什么。请帮忙

python loops while-loop simulation nested-loops
1个回答
0
投票

您的代码中的 elif 条件存在问题。条件 elif Money >= Profit_Goal 应该是一个独立的 if 语句,因为您想在每次旋转后检查此条件,而不仅仅是在满足 number % 2 == 0 条件时检查。

import random

Profit_Goal = int(input('Profit Goal: '))
Starting_Money = int(input('Starting money: '))
Table_Min = int(input('Table minimum bet: '))
Table_Max = int(input('Table maximum bet: '))
Bet = Table_Min
Money = Starting_Money

while 0 < Money < Profit_Goal:
    number = random.randint(1, 36)
    print('Spin:', number)
    if number % 2 != 0:
        Money = Money + Bet
        print('You won', Bet, 'on the bet. You have', Money, 'Now you have', Bet + Money)
    elif number % 2 == 0:
        Money = Money - Bet
        Bet = Bet * 2
        print('You lost', Bet, 'on the bet. You have', Money, 'Now you have', Money - Bet)
    if Money >= Profit_Goal:
        print('You reached your goal and beat the casino. You should stop now. You have', Money)
        break
else:
    print('You have lost all of your money.')
© www.soinside.com 2019 - 2024. All rights reserved.