这是我必须回答的问题:
编写程序以输入考试成绩(满分100分),直到您输入“退出”。完成考试成绩后,课程将打印出所有考试成绩的平均值。
HINTS: 要计算出平均值,您必须保持考试分数的总计和输入数字的计数。 尝试使程序工作一次,然后找出退出条件并使用while True:循环。
示例运行: 输入考试成绩或输入“退出”:100 输入考试成绩或输入“退出”:100 输入考试成绩或输入“退出”:50 输入考试成绩或输入“退出”:退出 得分数3.平均分为83.33
这是我的代码:
count = 0
average = sum
while True:
num =input('Enter an exam score or type "quit":')
count = count + 1
average = sum(num)
print ('the total of exams is %d and the average is %f' % count, average)
if num == 'quit':
break
print ('this is ok for now')
答案 0 :(得分:0)
这一行存在很多问题:public class MailParam {
private String username;
String pwd;
String auth;
String startls;
String host;
String port;
String from;
String replyto;
String logopath;
public String getAuth() {
return auth;
}
public String getHost() {
return host;
}
public String getPort() {
return port;
}
public String getPwd() {
return pwd;
}
public String getStartls() {
return startls;
}
public String getUsername() {
return username;
}
public String getFrom() {
return from;
}
public String getReplyto() {
return replyto;
}
public String getLogopath() {
return logopath;
}
public void setAuth(String val) {
auth = val;
}
public void setHost(String val) {
host = val;
}
public void setPort(String val) {
port = val;
}
public void setPwd(String val) {
pwd = val;
}
public void setStartls(String val) {
startls = val;
}
public void setUsername(String val) {
username = val;
}
public void setFrom(String val) {
from = val;
}
public void setReplyto(String val) {
replyto = val;
}
public void setLogoPath(String val) {
logopath = val;
}
}
。
average = sum(num)
的适当输入是要求和的数字列表。在您的情况下,您只需要一个数字或单词“quit”。
您需要做的是跟踪计数和总计,然后在最后进行划分:
sum
答案 1 :(得分:0)
我的观点是使用statistics
模块和sys
退出。还处理非int(或非浮点)输入并打印正确的消息,而不是退出ValueError
import statistics, sys
notes = []
count = 0
average = 0 # or None
while True:
note = input('Enter score: ')
# handle quit request first, skip any calculation
# if quit is the first request
if note == 'quit':
print('Program exit. Average is %s' % average)
sys.exit()
else:
# test if the entered data can be an int or float
try:
note = int(note) # can be replaced with float(note) if needed
count += 1 # increase the counter
notes.append(int(note))
average = statistics.mean(notes) # calculate the average (mean)
print('Total %s average is %s' % (count, average))
# just print an error message otherwise and continue
except ValueError:
print('Please enter a valid note or "quit"')