2
2

More than 1 year has passed since last update.

配列の要素の最大値、最小値、合計、平均

Posted at

python3で配列の要素の最大値、最小値、合計、平均を求めるプログラムを実装しました。

# 新規作成 2023/3/7
# 配列の最大、最小、平均、並び替え

scores = [30,20,70,40,10,50]
num = 0
pot = 0


while True:
  try:
    num1 = int(input("整数を1つ入力してください"))
    num = num1
    break
  except:
    print("整数を入力してください")

# 配列を1つ追加
scores.append(num1)

while True:
  try:
    w_n = len(scores)
    num2 = int(input(f"0から{w_n}までの整数を1つ入力してください"))
    if num2 < 0 or num2 > w_n:
      raise ValueError
    pot = num2
    break
  except:
    print("整数を入力してください")
# 配列の要素を1つ削除
scores.pop(pot)
print(scores)
#配列の要素の最大値
print(max(scores))
#配列の要素の最小値
print(min(scores))
#配列の要素の合計
print(sum(scores))
#配列の要素の平均
avg1 = round(sum(scores) / len(scores),1)
print(avg1)

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2