2016-04-08 22 views
1

質問は修正されました。他の重要な情報は忘れてしまいました。python 3xプログラムが正しく変数値を返す/表示していない

なぜ私のプログラムは授業料を計算できないのかと苦労しています。 ここに私のコードです。 のコードタイプ(pyhton、C++またはjava)を入力するようにユーザーに依頼することを意味します。 これは、ユーザーにレッスン数/追加時間を入力するよう依頼し、私が言うことができるものからうまくいく。 feecalc関数が問題を引き起こしていると信じていますが、feecalc関数が結果を出力するときに文字列を出力しますが、変数に値がないという事実以外に、実際のエラーは発生しません。またはその価値は変更されましたが、なぜこれが起こっているのかはわかりません。

多くのコードを貼り付けることをお詫びしますが、この問題の原因が「うまく」実行されているかどうかはわかりません。これは論理的なエラーですか、私は見落としているという単純な間違いをしましたか?

test data 
code = python 
number of lessons = 2 
additional hours = 3 
tests = 1 

表示全く計算された価格はありません見られるように

Welcome to Dr Ho Coaching Centre fee calculator 
what code do you wish to study? 
Python - Java - C++ 
>python 
thank you for choosing python 
How many lessons would you like?2 
How many additional hours would you like?3 
how many tests would you like to sit?1 

python 
Tuition fee for the lessons is $ 
Tuition fee for the additional hours is $ 
Tuition fee for the total tests is $ 
Total Tuition fee is $ 
Would you like to calculate the Tuition fee for another student? 

を次のように出力されます。私はこのフォーラム内で正しくフォーマットできなかったので、テストデータからいくつかの文字列を削除しました。 。任意の助けを事前に

def body(): 
    codeofstudy = '' 
    tlessons = '' 
    xhours = '' 
    tests = '' 
    totalcost = '' 
    lessoncost = '' 
    xhourscost = '' 
    testscost = '' 
    print(''' 
------------------------------------------------- 
Welcome to Dr Ho Coaching Centre fee calculator 
-------------------------------------------------''') 
codeofstudy = codecheck(codeofstudy) 
studytime(tlessons, xhours, tests) 
print(tlessons) 
feecalc(codeofstudy, tlessons, xhours, tests, lessoncost, xhourscost, testscost, totalcost) 
print('Tuition fee for the lessons is $',lessoncost) 
print('Tuition fee for the additional hours is $', xhourscost) 
print('Tuition fee for the total tests is $', testscost) 
print('Total Tuition fee is $', totalcost) 
rerun() 



def codecheck(codeofstudy): 
codeofstudy = input('''what code do you wish to study? 
Python - Java - C++ 
>''') 
if codeofstudy in {"python", "Python", "Java", "java", "C++", "c++"}: 
    print('thank you for choosing', codeofstudy) 
    return codeofstudy 
else: 
    print('That is not a valid answer') 
    codecheck(codeofstudy) 


def studytime(tlessons, xhours, tests): 
tlessons = input('How many lessons would you like?') 
xhours = input('How many additional hours would you like?') 
tests = input('how many tests would you like to sit?') 
tlessons = int(tlessons) 
xhours = int(xhours) 
tests = int(tests) 
return tlessons, xhours, test 


def feecalc(codeofstudy, tlessons, xhours, tests, lessoncost, xhourcost, testscost, totalcost): 
if codeofstudy in ("python", "Python"): 
    print('python') 
    lessoncost = tlessons * 300 
    xhourscost = xhours * 200 
    testscost = tests * 250 
    totalcost = lessoncost + xhourscost + testscost 
    return lessoncost, xhourscost, testscost, totalcost 

elif codeofstudy in ("java", "Java"): 
    print('java') 
    lessoncost = tlessons * 200 
    xhourscost = xhours * 150 
    testscost = tests * 150 
    totalcost = lessoncost + xhourscost + testscost 
    return lessoncost, xhourscost, testscost, totalcost 

elif codeofstudy in ("c++", "C++"): 
    print('C++') 
    lessoncost = tlessons * 175 
    xhourscost = xhours * 175 
    testscost = tests * 170 
    totalcost = lessoncost + xhourscost + testscost 
    return lessoncost, xhourscost, testscost, totalcost 

def rerun(): 
rerun = ' ' 
rerun = input('Would you like to calculate the Tuition fee for another student?') 
if rerun in ("y", "Y"): 
    body() 
body() 

感謝=]

答えて

0

あなたは「」値が空の文字列を持つすべての変数を宣言したが、あなたはデフォルト値でそれらを印刷します。 studytime() 3つのローカル変数に値を割り当てる(スコープはメソッドにのみ属します)、studytime()の外部に宣言された変数tlessons、xhours、およびtestには新しい値が割り当てられていません。あなたのようにコードを変更する必要が

:値

tlessons, xhours, test = studytime(tlessons, xhours, tests) 

リターンと

def test(x): 
    x = 10 
    return x 



x = 0 
test(x) 
print(x) # output is 0 

x = test(x) 
print(x) # output is 10 
+0

がhaifzhanありがとうたとえば、対応する変数に

を割り当てる、それはの一部であったが判明問題は、しかし、私はあなたがプログラムの他の部分に言ったものを適用し、それを働いた:) ありがとう百万(Y) – Skeg

+0

それは助けて嬉しい:) – haifzhan