下面是我的代码:
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
发布于 2018-06-19 07:39:13
首先,您使用的不是Python 3.x;您使用的是2.7 -您的结果只能在2.7中重现,而不能在3.x中重现。
其次,在这行中有整数除法
weighted_score = int((score_earned / 100) * weight_1)
在Python 2.7中,如果你用一个较小的整型数除以一个较大的整型数,你总是得到一个0。您的行必须是:
weighted_score = int((score_earned / 100.0) * weight_1) # Mind the .0
https://stackoverflow.com/questions/50918660
复制相似问题