调用方法时发生Python Traceback错误

时间:2018-06-21 08:51:27

标签: python python-3.x

我只是在玩弄python并得到一些我不理解的错误(似乎是相同的)。

所以这是我的便条纸

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的新手,只是想玩一点儿,所以我真的不知道如何解决这个问题。

任何帮助都会被爱。

1 个答案:

答案 0 :(得分:0)

play_again(bankAmount)

这是问题。再次播放不带任何参数。它实际上要求输入。由于您使用参数调用了此方法,因此会引发错误。

也是

choose_number(bankAmount)

由于choice_number需要一个参数,并且未在范围或play_again中定义库金额,因此,在play_again中调用main()而不是choose_number可使应用程序正常工作。

最后,作为学习新语言的一部分,您应该使堆栈跟踪您的最好的朋友。在某些情况下,错误消息可能是含糊不清的,在这种情况下,情况非常清楚。追溯的最后一行显示为

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