一个从用户那里获取大量数字的程序,当输入q时,它会计算总和最大值和平均值。
class pergunta {
constructor(perguntas = [], respostas = [], desafios = [], valor = 100, posicao = 0){
this.perguntas = perguntas;
this.respostas = respostas;
this.desafios = desafios;
this.posicao = posicao;
this.valor = valor;
jQuery.ajax({
url: '../php/consulta.php' + location.search,
type: "GET",
dataType: 'json',
success: function(pergunta, desafio){
this.perguntas = pergunta.pergunta;
this.respostas = pergunta.resposta;
this.desafios = desafio.descricao;
}
});
}
}
const perguntas = new pergunta();
console.log(perguntas);
答案 0 :(得分:0)
我会使用内置的Python函数:
nums = []
n = input("Please enter a number (or q to quit):")
while n not in {"Q","q","Quit"}:
nums.append(int(n))
n = input("Please enter a number (or q to quit):")
print("Count: {}, Max: {}, Min: {}, Sum: {}, Average: {}".format(len(nums),max(nums),min(nums),sum(nums),sum(nums)/len(nums)))
答案 1 :(得分:0)
所以这里有3个问题:
您需要使用raw_input
代替input
。
while n !="q" and "Q" and "Quit":
无效,因为它不会n
与"q"
,"Q"
和"Quit"
进行比较。它应该是while n !="q" and n != "Q" and n != "Quit":
如果只有一个号码,它将无效。最好将n = input("Please enter another number:")
放在while而不是第一行。
试试这个:
n = raw_input("Please enter a number:")
count = 0
min = None
max = 0
sum= 0
while n !="q" and n!="Q" and n!="Quit":
n1 = float(n)
if min is None:
min = n1
elif n1 < min:
min = n1
if n1 > max:
max = n1
sum += n1
count = count +1
n = raw_input("Please enter another number:")
print("Total:", sum)
print("Count:", count)
print("Min:", min)
print("Max:", max)
print("Average:", sum/count)