コンピューター サイエンスのクラスで、Python の関数とパラメーターについて学び始めたところです。今、私のインストラクターは私たちにパラメーターの受け渡しを学ばせています。プログラムの膨大な要約を入力する代わりに、下の課題ガイドを再入力しました。
説明:このプログラムでは、ユーザーはクレジット カードの料金の入力、支払いの入力、または残高の表示を選択する必要があります。ユーザーがキーボードでコードを入力して選択を示すことができるようにします。
次の関数名を使用します。
enterValue ユーザーが値を入力します
addCharge 関数に渡された値が残高に追加されます
addPayment 関数に渡された値が残高から差し引かれます
showBalance クレジット カードの現在の残高が表示されます
適切なアクションのために、ユーザーに次のコードを入力してもらいます。
料金を入力するための「C」
支払いを入力するための「P」
残高を示す「B」
「Z」が入力されるまでトランザクションの入力を許可する
プログラム
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. ")
出力
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
(私の問題は、内部の値enterValue()
が定義されていないことです。)