2016-05-08 6 views
1

私は給与の返品としてゼロを得る。私はそれが可変範囲と関係があるかもしれないことは知っていますが、私は迷っています。どのように私はwhileループで毎回累積するPythonで関数を作成しますか

''' 
Write a program to declare two empty lists one is for name and one for salaries. 
Within the for loop ask for employees name and their salaries 
and append them into the list accordingly. 
Find out the total salary using accumulation concept but you 
need to call a function called EvaluateSalary() within the for loop passing the argument 
salary for each iteration. 

Output : Both of the lists with their items and the total salary. 
''' 

#Declare name list 
nameList = [] 

#declare salary list 
salaryList = [] 

#declare quit constant 
QUIT = "ZZZ" or 000 

employeeName = str(input("Please enter an Employee name or ZZZ to quit :   ")).upper() 
employeeSalary = float(input("Please enter the salary of the Employee or 000 to quit : ")) 

以下は正しく行われていないものです。私はそれがループの各パスを介して給与の入力を追加したい。

salary = 0 
def EvaluateSalary(salary): 
    salary = salary + employeeSalary 
    return salary 

while employeeName != QUIT: 
    nameList.append(employeeName) 
    salaryList.append(employeeSalary) 
    EvaluateSalary(salary) 
    employeeName = str(input("Please enter an Employee name or ZZZ to quit : ")).upper() 
    employeeSalary = float(input("Please enter the salary of the Employee or 000 to quit : ")) 

print("Goodbye!") 
print(nameList, salaryList, salary) 
+0

、あなたのコードがそれはある – Pykler

+0

を動作するように適切にインデントする必要があります。私はちょうどここに正しく投稿する方法を知りません –

+0

それは自分のコードをフォーマットしていました –

答えて

1

あなたの問題はここにある:

salary = 0 
def EvaluateSalary(salary): 

    salary = salary + employeeSalary 
    return salary 

給与は、グローバル変数です。関数に渡す必要はないので、渡す必要があるのはemployeeSalaryです。

私は、これは給与がグローバルであるため、あなたが何かを返す必要はありません

salary = 0 
def EvaluateSalary(employeeSalary): 

    salary = salary + employeeSalary 

、あなたはどこでもあなたのコードからアクセスすることができます動作しますと思います。

方法2:グローバル変数を使用しない

def EvaluateSalary(employeeSalary, salary): 
    salary = salary + employeeSalary 
    return salary 

salary = 0 
while ...whatever...: 
    salary = EvaluateSalary(employeeSalary, salary) 
    (...) 

それで、あなたはwhileループの各反復でグローバルな給与の値を更新し、ちょうどaccummulatingを保ちます。

+0

私のコードを編集してくださったみなさん、@nhouserありがとうございます。これは私の最初の投稿でした。混乱のために申し訳ありません。 –

+0

@myhst私はあなたが示したものにコードを変更します。そして私はこれを事前に試みたと信じています。 UnboundLocalError:割り当て前にローカル変数 'salary'が参照されています –

+0

残念ながら、関数内のPythonでは、グローバル変数を指定する前にグローバルを指定する必要があります。 def行と他方の行の間に "グローバル給与"を書いてください。 – mhyst

2

スタックオーバーフローは、人にコードを書くよう依頼する場所ではありませんが、この記事は新しいプログラマーがコード例を見て、いくつかのテクニックを学ぶのに役立つと思います。その1つは、グローバルおよび/ 。私はあなたのコードを再書き込みした場合、次のように

、私はそれを記述します。これはPythonがない

def input_employees(): 
    while True: 
     name = input(
      "Please enter an Employee name or Hit Enter to quit : " 
     ).upper() 
     if not name: 
      break 
     salary = input(
      "Please enter the salary of the Employee or Hit Enter to quit : " 
     ) 
     if not salary: 
      break 
     yield {'name': name, 'salary': float(salary)} 

def sum_salary(employees): 
    # this is a guess from your code. my guess is you wanted a salary sum 
    return sum([e['salary'] for e in employees]) 

def main(): 
    employees = list(input_employees()) 
    print('Total Salaries:', sum_salary(employees)) 
    print(employees) 

main() # run the main function, ie the script 
+0

これは素晴らしいですね。助けてくれてありがとう。私はこのレベルになるまで待つことはできませんが、私はまだこれについて多くは理解していません。しかし、私はそこに着いて、あなたが私にここに示したことを勉強し続けます。 –

+0

@MichaelVivirito私の推測では、このコードの中で最も難しいのは、[yield statement](http://www.python-course.eu/generators.php)と[list comprehension](http://www.learnpython。 org/en/List_Comprehensions)。うまくいけば、それらのリンクはそれらの部分をクリアするでしょう。 – Pykler

関連する問題