Rooirokbokkie
Rooirokbokkie

Reputation: 252

Use user input within as input in a function Python

I'm currently having some trouble with this in Python 3. I've tried to figure out how to use the user input stored in income and pension_contribution in the pension() but nothing is happening. Where should those user inputs go in order to be considered input so the function can work?

income = input(int("What is your annual income?"))
pension_contribution = input(int("What is your monthly pension contribution?"))

def pension(pension_contribution, income):
    if pension_contribution > ((income//12) * .075):
        print ("Your pension contribution is too high. Monthly pension contributions may not exceed 7.5% of your gross monthly income")
    else:
        print("Your annual pension contribution is R", pension_contribution * 12)

Upvotes: 0

Views: 405

Answers (2)

logic
logic

Reputation: 1727

You could do this 3 different ways:

1. Simply call the function

def pension (pension_contribution, income):

    if pension_contribution > ((income//12) * .075):
        print ("Your pension contribution is too high. Monthly pension contributions may not exceed 7.5% of your gross monthly income")

    else:
        print("Your annual pension contribution is R", pension_contribution * 12)

income = int(input("What is your annual income?"))
pension_contribution = int(input("What is your monthly pension contribution?")) 

pension(pension_contribution, income) #call function

2. Put variables inside the function

def pension ():
    income = int(input("What is your annual income?"))
    pension_contribution = int(input("What is your monthly pension contribution?"))
    if pension_contribution > ((income//12) * .075):
        print ("Your pension contribution is too high. Monthly pension contributions may not exceed 7.5% of your gross monthly income")

    else:
        print("Your annual pension contribution is R", pension_contribution * 12)


pension() #still have to call function

2. Global variables (not recommended)

income = int(input("What is your annual income?"))
pension_contribution = int(input("What is your monthly pension contribution?"))

def pension ():
    global income, pension_contribution
    if pension_contribution > ((income//12) * .075):
        print ("Your pension contribution is too high. Monthly pension contributions may not exceed 7.5% of your gross monthly income")

    else:
        print("Your annual pension contribution is R", pension_contribution * 12)

pension() #still have to call function

Notice how you have to actually "call" the function every time. Otherwise the program would run successfully but never bother entering the function.

Upvotes: 1

Ashoka Lella
Ashoka Lella

Reputation: 6729

1) Your indentation seems to be off
2) You need to call the function

income = input(int("What is your annual income?"))
pension_contribution = input(int("What is your monthly pension contribution?"))

def pension (pension_contribution, income):

    if pension_contribution > ((income//12) * .075):
        print ("Your pension contribution is too high. Monthly pension contributions may not exceed 7.5% of your gross monthly income")

    else:
        print("Your annual pension contribution is R", pension_contribution * 12)

pension(pension_contribution, income)

Upvotes: 2

Related Questions