pythonpositional-argument

Interpreter Says that I am missing a positional argument


I am doing a project on passing arguments through functions. My problem is that I am writing a program that gives the charged amount based on age and traffic violations. Here is my current code:

print("Use this program to estimate your liability.")

def main():
    user_name()
    age()
    violations()
    risk_code()
#Input#

#Def for name
    
def user_name():
    user_name = print(input("What is your name?"))
    
#Def for age
def age():
    age = int(input("What is your age?"))
    
#Def for traffic violations (tickets.)
def violations():
    violation = print(input("How many traffic violations (tickets) do you have?"))
   
#Process#
def risk_code(violation):
    if violation == 0 and age >= 25: 
        risk = "None"
        cost = int(275)
#How many tickets to indicate risk code (therefore risk type)

# Age + traffic violations (tickets) = risk code

# Age + Traffic violations + Risk Code = Price

#Output#

#Def for customer name

# Def for risk output

# Def for cost
main()

I want the program to display how much the customer owes if I were to select my age as 25, with zero violations. The issue is I keep getting a positional argument error. I am a little confused on what this means. Could anyone provide help/example?


Solution

  • You have several issues in your code:

    1. The positional argument error is caused because you call risk_code() func without providing it the argument it needs: violation.

    2. user_name = print(input("What is your name?")) - user_name will be None since print function returns nothing. Actually, you don't need to print in order to output your message, input does it for you.

    3. You have to add return to your functions in order to be able to pass your variables which are defined inside the function scope into other functions. For example, in violations() function, violation variable is defined inside the scope of the function and without returning it, you won't be able to use it somewhere else in your code.

    I made some changes in your code, try it:

    print("Use this program to estimate your liability.")
    
    
    def main():
        user_name = get_user_name()
        user_age = get_age()
        violation = get_violations()
        risk_code(violation, user_age)
    
    
    # Input#
    
    # Def for name
    
    def get_user_name():
        user_name = input("What is your name?")
        return user_name
    
    
    # Def for age
    def get_age():
        user_age = int(input("What is your age?"))
        return user_age
    
    
    # Def for traffic violations (tickets.)
    def get_violations():
        violation = input("How many traffic violations (tickets) do you have?")
        return violation
    
    
    # Process#
    def risk_code(violation, age):
        if violation == 0 and age >= 25:
            risk = "None"
            cost = int(275)
            print("cost:", cost)
            print("risk:", risk)
    
    
    # How many tickets to indicate risk code (therefore risk type)
    
    # Age + traffic violations (tickets) = risk code
    
    # Age + Traffic violations + Risk Code = Price
    
    # Output#
    
    # Def for customer name
    
    # Def for risk output
    
    # Def for cost
    main()
    

    There are still improvements to do (user_name is unused for example) but it might be a good start.