0

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?

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • 1
    Method call `risk_code()` and definition are not in sync `def risk_code(violation):` – Adarsh Oct 04 '20 at 05:34
  • https://stackoverflow.com/questions/9450656/positional-argument-v-s-keyword-argument#:~:text=Positional%20arguments%20are%20arguments%20that,must%20passed%20to%20the%20function.&text=In%20python%20optional%20arguments%20are%20arguments%20that%20have%20a%20default%20value. – RCvaram Oct 04 '20 at 05:36
  • In your own words, how are you expecting the `risk_code` function to know what value of `violation` it should use? – Karl Knechtel Oct 04 '20 at 05:46

2 Answers2

0

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()
RCvaram
  • 2,052
  • 2
  • 7
  • 22
0

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.

Gabip
  • 6,159
  • 3
  • 5
  • 19
  • 1
    Hey! Code works, I just need to add a print value saying the cost, etc. I should have caught that simple print(input) mistake. I feel stupid :( Thank you for the help! Functions are definitely harder for me to grasp right now. So to clarify, under def main(), we need to actually assign our local variables to our functions to be passed? – Blake Bratu Oct 04 '20 at 05:48
  • Just to add on though, the print does not work in the statement. – Blake Bratu Oct 04 '20 at 06:00
  • Your local variables (which are defined inside your functions scope) should be returned (take a look how I did it in `get_age()` for example) and only then they can be passed as arguments to other functions. – Gabip Oct 04 '20 at 06:00
  • According to your logic, it should work only if `age >= 25` and `violation == 0`. Try to input age=26 and violation=0 and you will see that it works. – Gabip Oct 04 '20 at 06:02