Python中的老虎机游戏

2 投票
3 回答
32 浏览
提问于 2025-04-13 01:41

我正在用Python制作一个非常简单的老虎机。这是我写的第一段Python代码,我在跟着一个YouTube教程学习。不知道为什么,当我在终端测试代码时,结果总是不对。

MAX_LINES = 3

def deposit():
    while True:
        amount = input("What would you like to deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be greater than 0.")
        else:
                    print("Please enter a number.")

                    return amount



def get_number_of_lines():
      while True:
        lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter a valid number of lines.")
        else:
                    print("Please enter a number.")

                    return lines


def main():
    balance = deposit()
    lines = get_number_of_lines()
    print(balance, lines)

main()

这是我到目前为止写的代码。

这是我在终端看到的结果(我是在VsCode里做的)。

What would you like to deposit? $200

Enter the number of lines to bet on (1-3)? 3

None None. \<\<this should say 3 ... 

我还没有找到进一步的解决办法,但我在寻找这个问题的答案时遇到了困难。在YouTube视频下也有其他人遇到同样的终端输出问题。

我运行调试时屏幕的照片

3 个回答

0

你需要正确缩进你的 return ... 行:

MAX_LINES = 3

def deposit():
    while True:
        amount = input("What would you like to deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be greater than 0.")
        else:
                    print("Please enter a number.")

    return amount

def get_number_of_lines():
    while True:
        lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter a valid number of lines.")
        else:
                    print("Please enter a number.")

    return lines

def main():
    balance = deposit()
    lines = get_number_of_lines()
    print(balance, lines)

main()

抱歉,是的,def get_number_of_lines(): 中的 while True: 也缩进错了。

1

你写的两个函数里的 return 语句缩进得太深了。当代码执行到 break 语句时,函数会返回,但没有把任何值传回给调用它的地方,所以捕获的变量被设置成了 None。这两个 return 语句应该和控制循环的 while 保持同样的缩进级别,这样在触发 break 的时候,它们才能真正返回一个值。

def deposit():
    while True:
        amount = input("What would you like to deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be greater than 0.")
        else:
                    print("Please enter a number.")

    return amount



def get_number_of_lines():
    while True:
        lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter a valid number of lines.")
        else:
                    print("Please enter a number.")

    return lines
1

你只需要在while循环结束后再返回结果。还有,请尽量保持默认的缩进级别为4个空格或者一个制表符。

MAX_LINES = 3

def deposit():
    while True:
        amount = input("What would you like to deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be greater than 0.")
        else:
                    print("Please enter a number.")

    return amount



def get_number_of_lines():
    while True:
        lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter a valid number of lines.")
        else:
                    print("Please enter a number.")

    return lines


def main():
    balance = deposit()
    lines = get_number_of_lines()
    print(balance, lines)

main()

撰写回答