调用方法时发生Python回溯错误

2024-10-01 13:27:42 发布

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

我只是在玩弄python,得到了一些错误(看起来是一样的),我不明白

这是我的sript:

import random
import os


def choose_number(bankAmount):

    print("Your bank amount:",bankAmount,"$")
    betAmount = float(input("Enter your bet amount: "))
    while (betAmount > bankAmount) | (betAmount <= 0):
        if betAmount > bankAmount:
            print("Your bet amount can't be higher then your bank amount!")
            betAmount = float(input("Please enter your bet amount again: "))
        else:
            print("Your bet amount can't be 0 or under.")
            betAmount = float(input("Please enter your bet amount again: "))
    bankAmount -= betAmount

    playerChance = str(input("Please choose your winchance from high, medium and low: "))
    while (playerChance != "high") & (playerChance != "medium") & (playerChance != "low"):
        print("You have to choose from 'high', 'medium or 'low'")
        playerChance = str(input("Please choose your winchance again: "))
    playerRoll = 0
    winAmount = 0.0
    if playerChance == "high":
        playerRoll = 60
        winAmount = betAmount * 1.5
    elif playerChance == "medium":
        playerRoll = 25
        winAmount = betAmount * 3.5
    else:
        playerRoll = 5
        winAmount = betAmount * 7.5

    roll_number(bankAmount, betAmount, playerRoll, winAmount)


def roll_number(bankAmount, betAmount, playerRoll, winAmount):

    random.seed
    rolledNumber = random.randint(1,100)

    if rolledNumber <= playerRoll:
        bankAmount += winAmount
        print("You won!\nWith a chance of",playerRoll,"% you won",winAmount,"$.\nThe rolled number were:",rolledNumber,".\nCongratulations!")
    else:
        print("You lost!\nThe rolled number were:",rolledNumber,".")

    play_again(bankAmount)


def play_again():

    playAgain = int(input("If you want to play again, enter 1: "))
    if playAgain == 1:
        choose_number(bankAmount)


def main():

    bankAmount = float(input("Enter your bank amount: "))
    while bankAmount <= 0:
        print("Your bank amount can't be under 0.")
        bankAmount = float(input("Please enter your bank amount again: "))

    choose_number(bankAmount)


main()

到目前为止,它还可以工作,但仍然给我一些错误:

Enter your bank amount: 500
Your bank amount: 500.0 $
Enter your bet amount: 50
Please choose your winchance from high, medium and low: high
You won!
With a chance of 60 % you won 75.0 $.
The rolled number were: 26 .
Congratulations!
Traceback (most recent call last):
  File "/Users/admin/Projekte/moneyMultiplier.py", line 68, in <module>
    main()
  File "/Users/admin/Projekte/moneyMultiplier.py", line 65, in main
    choose_number(bankAmount)
  File "/Users/admin/Projekte/moneyMultiplier.py", line 34, in choose_number
    roll_number(bankAmount, betAmount, playerRoll, winAmount)
  File "/Users/admin/Projekte/moneyMultiplier.py", line 48, in roll_number
    play_again(bankAmount)
TypeError: play_again() takes 0 positional arguments but 1 was given

因为我对python有点陌生,只是想玩一玩,所以我真的不知道如何解决这个问题

任何帮助都会受到欢迎


Tags: numberinputyouramountbankprintpleaseagain
1条回答
网友
1楼 · 发布于 2024-10-01 13:27:42
play_again(bankAmount)

这就是问题所在。再次播放不接受任何参数。它实际上要求输入。因为您使用参数调用了此方法,所以它会抛出一个错误

还有

choose_number(bankAmount)

由于choose\u number需要一个参数,并且在作用域中未定义银行金额,因此在play\u中再次调用main()而不是choose\u number可以使应用程序正常工作

最后,作为学习一门新语言的一部分,你应该让stack trace成为你最好的朋友。虽然有时错误消息可能是神秘的,但在本例中,这是非常清楚的。你回溯的最后一行是

TypeError: play_again() takes 0 positional arguments but 1 was given

相关问题 更多 >