2016-10-16 15 views
0

これはこれまでの私のコードです。私は4つのテストのスコアを入力するようにユーザーに促すコードを得ることができます。 「テスト番号Nのスコアを入力してください」と尋ねるようにしました。配列を使用するようにコードを変更するにはどうすればよいですか?

私の問題は、ユーザーが入力するものから入力値を抽出できることにあります。私の最終目標は、最も低いテストグレードを落とし、残りのスコアで平均を計算できるようにすることです。使用される入力データは99,99,99、および77です。

私は、X番号が割り当てられた変数からminを取りますが、ユーザー入力から取得する場合は取得しません。

def main(): 
    scores = getNumbers() 

def getNumbers(): 
    testCountIs = 0 
    testNum = 4 
    totalScore = 0 

    for test in range(1, testNum+1): 
     print('Enter the score for test number', int(testCountIs+1), end='') 
     testScores = int(input(': ')) 
     testCountIs += 1 

main() 

Edit2:以下はこのプログラムで動作するコードです。

def main(): 
     scores = getNumbers() 
     print("The average with the lowest score dropped is: ",float(sum(scores))/len(scores)) 

    def getNumbers(): 
     scores=[] 
     for testNum in range(4): 
      print('Enter the score for test number',int(testNum+1),end='') 
      scores.append(int(input(': '))) 
     scores=[score for score in scores if min(scores)!=score] 
     return scores 

    main() 
+1

の平均外を見つける必要がありますだけで

それを返す
scores = [] for test in range(testNum): print('Enter the score for test number', int(test + 1), end='') score = int(input(': ') scores.append(score) return scores 

そして、それに追加し、リストを定義する必要がありますこれは良い出発点でなければなりません://www.tutorialspoint.com/python/python_lists.htm – NPE

+4

私はあなたがリストを意味すると思う... pythonには配列がありますが、 '[]'はリストと呼ばれます –

+0

ループがゼロから始まる方が理にかなっています。 'test_score [test]'を割り当てます。基本的にはループインデックスを複製するだけなので、カウントは必要ありません。 – tripleee

答えて

0

これは何をしたいです:

def getNumbers(): 
    scores=[] 
    for testNum in range(4): 
     print('Enter the score for test number %d'%(testNum+1)), 
     scores.append(input(': ')) 
    scores=[score for score in scores if min(scores)!=score] 
    return scores 

scores= getNumbers() 
average= float(sum(scores))/len(scores) 
print average 

編集のpython3

def getNumbers(): 
    scores=[] 
    for testNum in range(4): 
     print('Enter the score for test number',int(testNum+1),end='') 
     scores.append(int(input(': '))) 
    scores=[score for score in scores if min(scores)!=score] 
    return scores 

scores= getNumbers() 
print(float(sum(scores))/len(scores)) 
+0

なぜコロンで分割する必要がありますか?私は質問がちょうど 'testNum'値の入力を要求されたと思っています。入力は' 1:2:3'ではありません。 –

+0

答えは "入力のスコアが次のように区切られていると仮定します:" – yabhishek

+0

私はそれを理解しました。私のポイントでした。 1行の入力が欲しいとは思われません –

0

のためのあなたのコードでは、近くにありました。 HTTPS:あなたは、最小の計算にドロップして、この機能

+0

入力されるリストをどのように追加するのですか? – Alina

+0

あなたは何を意味するのか分かりません。追加する前に入力を取得します。私はちょうど1つの行で両方をしました。しかし、私はそれを明確にするために編集しました –

関連する問題