简体   繁体   中英

Python Interpreter Says that I am missing a positional argument. I am a little new to passing arguments and confused

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?

You didn't return anything from the function and aswell you didn't pass any argument to the function


def risk_code(violation, age ):
 if violation == 0 and age >= 25: 
  risk = "None"
  cost = int(275)
  return risk, cost
def main():
 user_name = input("What is your name?")
 age = int(input("What is your age?"))
 violation =input("How many traffic violations (tickets) do you have?")
 risk, cost = risk_code(violation,age)
 print(f" Risk :{risk} and cost {cost}")


main()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM