-1

私はPythonの初心者です。私は自分の関数を定義して、顧客がチケットの割引を受け取るかどうかを決定するプログラムを作成しています。最初に私は彼らが事前に登録しているかどうかを尋ね、if-then-elseの決定書を持っている私の機能を呼び出すか、10%の割引を適用するかしないかを尋ねます。私は私のプログラム、何か提案に間違って何をしているのかは不明です。 編集:私の出力は0 $を返します。事前登録していない場合は20 $、事前登録の場合は20 $の10%を計算したいと考えています。Python - 決定文と戻り変数を使用して関数を定義する

def determineDiscount(register, cost): 
#Test if the user gets a discount here. Display a message to the screen either way. 
    if register == "YES": 
     cost = cost * 0.9 
     print("You are preregistered and qualify for a 10% discount.") 
    else: 
     print("Sorry, you did not preregister and do not qualify for a 10% discount.") 
    return cost 

#Declarations 
registered = '' 
cost = 0 
ticketCost = 20 

registered = input("Have you preregistered for the art show?") 
determineDiscount(registered, ticketCost) 

print("Your final ticket price is", cost, "$") 
+1

ご質問はありますか?インデントがオフになっているので、実際に実行しているコードを反映するように修正してください。あなたは、「私がこのようなことをしたときなど、そのようなエラーが発生すると、完全なトレースバックです...」というような問題文を明確にする必要があります。インデントが正しいと仮定すると、私はすぐにあなたがあなたの関数が返す値を取得します。したがって、「コスト」は常に「0」になります –

+0

質問を明記してください。実行されない場合は、エラースタックを提供してください。 – Musen

+0

もう一つ、あなたが使用するPython。 2.7ではinput()はありませんが、python3ではそうです。 最終的にすべての入力を大文字に変換します。今すぐ "はい"を入力すると割引があります。 "はい"と入力すると、それを持っていません。 – darvark

答えて

0

コードがPY3にPY2でraw_inputinputを使用する必要があります。 また、関数によって返されるコストは、コストに格納する必要があります。それ以外の場合は、変更されません。関数外のコストは、関数内のコストと同じではありません。

def determineDiscount(register, cost): 
    if register.lower() == "yes": 
     cost *= 0.9 
     print("You are preregistered and qualify for a 10% discount.") 
    else: 
     print("Sorry, you did not preregister and do not qualify for a 10% discount.") 
    return cost 


ticketCost = 20 
registered = raw_input("Have you preregistered for the art show?") 
cost = determineDiscount(registered, ticketCost) 

print("Your final ticket price is", cost, "$") 
0
def determineDiscount(register, cost): 
#Test if the user gets a discount here. Display a message to the screen either way. 
    if register.lower() == "yes": #make user's input lower to avoid problems 
     cost -= cost * 0.10 #formula for the discount 
     print("You are preregistered and qualify for a 10% discount.") 
    else: 
     print("Sorry, you did not preregister and do not qualify for a 10% discount.") 
    return cost 
#Declarations 
ticketCost = 20 
#get user input 
registered = input("Have you preregistered for the art show?") 
#call function in your print() 
print("Your final ticket price is $", determineDiscount(registered, ticketCost)) 

出力:

Have you preregistered for the art show?yes 
You are preregistered and qualify for a 10% discount. 
Your final ticket price is $18 
関連する問題