[Python] I don't know how to call my variable outside of my function [duplicate]

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    Below is my code, as well as the error message, and if you run it then it gives an error saying paycheck is not defined and I don't know how to make paycheck definable.

    I tried to put the variable paycheck inside of def totalbal(paycheck): but it refuses to recognize it. I was expecting it to be able to take the paycheck variable from the fucntion paycheckfunc(): but it's obviously not capable of that. I think I might need to just make a separate function for paycheck but I am not sure

    Error message:

    Traceback (most recent call last):
    File "C:\Users\RedeemerAV\PycharmProjects\PythonProject\.venv\Budget.py", line 45, in <module>
    main()
    ~~~~^^
    File "C:\Users\RedeemerAV\PycharmProjects\PythonProject\.venv\Budget.py", line 9, in main
    totalbal(paycheck)
    ^^^^^^^^
    NameError: name 'paycheck' is not defined


    CODE:

    balance = 0



    def main():
    preface()
    paycheckfunc()
    phonebill()
    totalbal(paycheck)


    def preface():
    print("This will help you calculate your weekly budget")
    print("Please use whole numbers, thank you")
    # would like to change this but don't know how
    print()


    def paycheckfunc():
    hrs = int(input("How many hours did you work this week?\n"))
    mphr = int(input("How much do you get paid per hour?\n"))
    paycheck = round(((balance + (hrs * mphr)) * .79), 2)
    print()
    print()
    # defeinetly has to be a better way than this to skip lines lol
    print("Below is your paycheck for this week")
    print()
    print(paycheck)


    def phonebill():
    pbill = int(input("How much is your phone bill on a monthly basis?\n"))
    pbillw = print(pbill * .25)
    print("This is your phone bill on a weekly basis")
    print(pbillw)


    def totalbal(paycheck):
    Inc = (paycheck)
    Exp = (pbillw)
    tbal = (Inc - Exp)
    print("This is your total balance")


    main()

    Continue reading...

Compartilhe esta Página