简体   繁体   中英

Functions and parameters in python 2.7.3

In my Computer Science class, I am just starting to learn about Functions and parameters in python. Right now my instructor is making us learn parameter passing. Instead of typing a huge summarization of my program I just retyped the assignment guide down below.

Description: In this program the user will have to option of entering a charge, entering a payment or displaying the balance on their credit card. Allow the user to indicate their choice by entering a code at the keyboard.

Use the following function names:

  • enterValue the user enters a value

  • addCharge the value passed to the function is added to the balance

  • addPayment the value passed to the function is subtracted from the balance

  • showBalance the current balance on the credit card is displayed

Have the user enter the following codes for the appropriate actions:

  • "C" for entering charges

  • "P" for entering payments

  • "B" for showing the balance

  • Allow the entry of transactions until "Z" is entered

Program

balance = 0
def enterValue ():
    enter = input ("Enter a value.")
    return enter

def addCharge (enter,balance):
    balance = balance + enter
    return balance

def addPayment (enter,balance):
    balance = balance - enter
    return balance
def showBalance ():
    print "Your balance is... ", balance


transaction = raw_input ("Enter C for charges, P for payments, and B to show your balance. ") 
enterValue ()
while transaction != "Z":


    if transaction == "C":
        balance = addCharge(enter,balance)
        showBalance()        
    elif transaction == "P": 
        balance = addPayment (enter,balance)
        showBalance()
    elif transaction =="B":
        balance = enterValue()
        showBalance()
    transaction = raw_input ("Enter C for charges, P for payments, and B to show your balance. ") 

Output

Enter C for charges, P for payments, and B to show your balance. P

Traceback (most recent call last):
  File "/Users/chrisblive/Downloads/Charge_Braverman-2.py", line 26, in <module>
    balance = addPayment (enter,balance)
NameError: name 'enter' is not defined

(My problem is that my value inside of enterValue() is not being defined.)

The main charge of the exercise is to understand passing parameters to functions. So just pass all needed variables in the functions to it! Approximately you can say that all functions have their own namespaces an if you want to use a value of another level in it, you have to pass it as a parameter and return it if you want to reuse it in a lower level.

For example:

###   Level "enterValue"   ###
def enterValue():
    return float(raw_input("Enter a value: "))
### End Level "enterValue" ###

###   Level "addCharge"   ###
def addCharge(enter, balance):
    balance = balance + enter
    return balance
### End Level "addCharge" ###

###   Level "showBalance"   ###
def showBalance(balance):
    print "Your balance is %f" % balance
### End Level "showBalance" ###

### Level "Mainlevel" ###
# This is where your program starts.
transaction = None
balance = 0.0
while transaction != "Z":
    transaction = raw_input("Enter C for charges, P for payments, and B to show your balance.\nEnter Z to exit: ").upper()

    if transaction == "C":
        enter = enterValue()
        balance = addCharge(enter, balance)
        showBalance(balance)
    elif transaction == "P":
        balance = addPayment(enter, balance)
        showBalance(balance)
    elif transaction == "B":
        showBalance(balance)
### End Level "Mainlevel" ###

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