这是我的代码:
def midterm_1():
print("Midterm 1:")
weight_1=int(input("Weight 0-100?"))
score_earned=int(input("Score earned?"))
score_shift=int(input("Were scores shifted (1=yes 2=no)?"))
if (score_shift==1):
shift_amount=int(input("What was the shift amount?"))
score_earned=int(shift_amount)+int(score_earned)
if (score_earned >100):
score_earned=100
print("Total points = "+str(score_earned)+str("/100"))
weighted_score=int((score_earned/100)*weight_1)
print("Your weighted score = "+str(weighted_score)+"/"+str(weight_1))
此代码应该是用于计算等级的较大代码的一部分。当打印加权分数时,它仅将score_earned视为100或0。
我该如何解决这个问题?
以下是没有得分时的输出示例:
Midterm 1:
Weight 0-100? 50
Score earned? 78
Were scores shifted (1=yes 2=no)? 2
Total points = 78/100
Your weighted score = 0/50
当分数转换且score_earned超过100时:
Midterm 1:
Weight 0-100? 89
Score earned? 89
Were scores shifted (1=yes 2=no)? 1
What was the shift amount? 90
Total points = 100/100
Your weighted score = 89/89
答案 0 :(得分:1)
首先,你没有使用Python 3.x;你使用的是2.7 - 你的结果只能在2.7中复制,但不能在3.x中复制。
其次,你在行
中有整数除法weighted_score = int((score_earned / 100) * weight_1)
在Python 2.7中,如果将较小的int数除以较大的int数,则总是得到0.您的行必须是:
weighted_score = int((score_earned / 100.0) * weight_1) # Mind the .0
答案 1 :(得分:0)
你应该退后一步,看看问题的各个部分
类似这样的事情
def get_integer(prompt,min_val=float("-inf"),max_val=float("inf")):
while True:
try:
result = int(input(prompt))
except (TypeError,ValueError):
print("That is not an integer... try again")
continue
else:
if min_val < result < max_val:
return result # guaranteed to be an integer
print("Please Enter a value Between {min_val} - {max_val}".format(min_val=min_val,max_val=max_val))
类似这样的事情
def get_input(exam_name):
""" guaranteed to return 3 integers between 0 and 100 """
print("Enter Scores For : {exam}".format(exam=exam_name))
return get_integer("Weight:",0,100),get_integer("Score:",0,100),get_integer("Offset(or zero for none):",0,100)
类似
def midterm1():
wt,score,offset = get_input("Midterm 1")
score = min(score+offset,100) # if score + offset > 100 then just use 100
return wt,score
但这些观点实际上都没有解决你的问题....这在@dyz回答
中得到了解决