Python:基于值和频率输入的平均值
Python: Average based on Input of Values and Frequencies
我正在尝试编写一个 python 函数 number_pairs,它使用自然正数 n,并读取 n 对自然正数来自用户。每对代表一个值及其频率。对于每一对,该函数必须提示用户输入两个正整数值及其频率,同时指示预期对的索引。重复该过程,直到输入了所有 n 对。最后,该函数应打印 n 对数字的平均值(Float 类型,具有示例中的确切字符串消息),以及 returns 平均值。您可以假设用户只输入了有效数据。
我当时在想,也许可以编写一个执行累积递归的辅助函数,但我错过了很多讲座,而且我不知道该怎么做。这是我目前所拥有的:
def averge_h(counter):
...
def number_pairs(n):
prompt1 = "Enter value for pair number "
prompt2 = "Enter its frequency:\n"
pn = "{0}: ".format(n)
res="Their average is: "
v = int(input(prompt1+pn))
f = int(input("Enter its frequency: "))
if n = 1:
average = (v*f)/f
else:
v = v+1
print res + str(average)
return average
您可以尝试这样的操作:
def read_and_avg(sum_,n,left,i): ## left is the number of times the input is to be taken
if left == 0:
print (sum_/float(n))
return (sum_/float(n))
else:
i = i + 1
print "Enter the values for pair number "+str(i)
a = int(input())
b = int(input())
sum_ = sum_ + (a*b) ## Increment the sum
n = n + b ## Increment the total count
print sum_,n,left
return read_and_avg(sum_,n,left-1,i) ## Decrease left by 1,
def func(n):
read_and_avg(0,0,n,0)
既然你说它只能有一个参数"n"看看这个:
def number_pairs(n):
if n == 0:
return 0
else:
average = number_pairs(n-1)
print str(n) + ("st" if n == 1 else ("nd" if n == 2 else ("d" if n == 3 else "th"))) + " pair"
val = input("value: ")
frq = input("frequency: ")
print "" # added for new line
return average + (val * frq)
n = input("Enter the number of pairs: ")
print "" # added for new line
print "The average is: " + str(number_pairs(n) / n)
Output:
Enter the number of pairs: 5
1st pair
value: 1
frequency: 2
2nd pair
value: 2
frequency: 2
3d pair
value: 3
frequency: 2
4th pair
value: 4
frequency: 2
5th pair
value: 5
frequency: 2
The average is: 6
我正在尝试编写一个 python 函数 number_pairs,它使用自然正数 n,并读取 n 对自然正数来自用户。每对代表一个值及其频率。对于每一对,该函数必须提示用户输入两个正整数值及其频率,同时指示预期对的索引。重复该过程,直到输入了所有 n 对。最后,该函数应打印 n 对数字的平均值(Float 类型,具有示例中的确切字符串消息),以及 returns 平均值。您可以假设用户只输入了有效数据。
我当时在想,也许可以编写一个执行累积递归的辅助函数,但我错过了很多讲座,而且我不知道该怎么做。这是我目前所拥有的:
def averge_h(counter):
...
def number_pairs(n):
prompt1 = "Enter value for pair number "
prompt2 = "Enter its frequency:\n"
pn = "{0}: ".format(n)
res="Their average is: "
v = int(input(prompt1+pn))
f = int(input("Enter its frequency: "))
if n = 1:
average = (v*f)/f
else:
v = v+1
print res + str(average)
return average
您可以尝试这样的操作:
def read_and_avg(sum_,n,left,i): ## left is the number of times the input is to be taken
if left == 0:
print (sum_/float(n))
return (sum_/float(n))
else:
i = i + 1
print "Enter the values for pair number "+str(i)
a = int(input())
b = int(input())
sum_ = sum_ + (a*b) ## Increment the sum
n = n + b ## Increment the total count
print sum_,n,left
return read_and_avg(sum_,n,left-1,i) ## Decrease left by 1,
def func(n):
read_and_avg(0,0,n,0)
既然你说它只能有一个参数"n"看看这个:
def number_pairs(n):
if n == 0:
return 0
else:
average = number_pairs(n-1)
print str(n) + ("st" if n == 1 else ("nd" if n == 2 else ("d" if n == 3 else "th"))) + " pair"
val = input("value: ")
frq = input("frequency: ")
print "" # added for new line
return average + (val * frq)
n = input("Enter the number of pairs: ")
print "" # added for new line
print "The average is: " + str(number_pairs(n) / n)
Output: Enter the number of pairs: 5 1st pair value: 1 frequency: 2 2nd pair value: 2 frequency: 2 3d pair value: 3 frequency: 2 4th pair value: 4 frequency: 2 5th pair value: 5 frequency: 2 The average is: 6