1

我试图打破循环说

if deff <= int(total):
    break

但是无论输入为负数还是大于总和,循环都会中断,它将中断循环

关于我做错了什么的任何建议?

PS我知道我会新的一个公式来决定玩家是赢还是输。现在我只是想在去那里之前弄清楚第一个代码

第一次编程,老师没有帮助):


def intro():

    greeting()

    print('How much money do you want to start with?')

    print('Enter the starting amount of dollars.', end='')
    total = int(input())

    print('Your current total is '+ str(total)+'\n')
    while True:

        print('How much money do you want to bet (enter 0 to quit)?', end='');
    # Bett will be the amount of money that the player will play with

        bett = int(input())
        if bett > int(total):
            print('ERROR You don\'t have that much left')

        if bett < int(0):
            print('ERROR: Invalid bet amount\n')

        if bett <= int(total)
            break

# Function shows results of slot machine after handle being pulled
def random():
    import random

    num1 = random.randint(1, 5)
    num2 = random.randint(1, 5)
    num3 = random.randint(1, 5)

    print('/---+---+---\  ')
    print('|-'+ str (num1)+'-|-'+ str(num2) +'-|-'+ str (num3) +'-|')
    print('\---+---+---/  ')



intro()

4

3 回答 3

2

您需要在连续的条件测试中使用elifand :else

    bett = int(input())
    if bett > total:
        print('ERROR You don\'t have that much left')

    elif bett < 0:
        print('ERROR: Invalid bet amount\n')

    else:
        break

这样,只执行一条语句,而不是更多或更多。

注意:没有必要一直使用int()构造函数来处理已经是int

于 2013-02-21T23:41:01.287 回答
0

在这个块中:

    if bett <= int(total)
        break

您有语法错误。在第一行的末尾添加一个冒号:

    if bett <= int(total):
        break
于 2013-02-21T23:38:40.297 回答
0

如果是我,我会使用 while 循环。

play = True
while play == True:
    #all the code
    #to be executed
    #indented here
    #
    #Ask user if they want to continue playing
    p = input("Play again?[y/n] ")
    playAgain = str(p)

    if playAgain == "y":
        play = True
    else:
        play = False
于 2013-03-07T23:59:51.153 回答