Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

Average Innings Score in python

I try the code for this program in c++ , it successfully completed all the test cases. but in python language it shows an value error for same logic.How to solve this ? [1]: https://i.stack.imgur.com/vVLmv.jpg My code :

def avg(s,n):
  sum1=0
  sum2=0
  for i in range(0,n):
      for j in range(0,2):
          if j%2==0:
             sum1+=s[i][j]
          else:
             sum2+=s[i][j]
  print((float)(sum1/n))
  print((float)(sum2/n))
n=int(input())
c=2
s=[[0 for j in range(0,c)]for i in range(0,n)]
for i in range(0,n):
    for j in range(0,c):
        s[i][j]=int(input())
avg(s,n)
question from:https://stackoverflow.com/questions/65870852/average-innings-score-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The way you are taking input is wrong

When you do this s[i][j]=int(input()) you are converting two space separated ints to a single int

def avg(s,n):
  sum1=0
  sum2=0
  for i in range(0,n):
      for j in range(0,2):
          if j%2==0:
             sum1+=s[i][j]
          else:
             sum2+=s[i][j]
  print((float)(sum1/n))
  print((float)(sum2/n))
n=int(input())
c=2
# I am going to ask for user input n times, split the input every time and convert individual score to int 
s=[list(map(int, input().split())) for _ in range(n)]
avg(s,n)
2
100 200
200 300
150.0
250.0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...