首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python忽略成绩计算器中的if语句

python忽略成绩计算器中的if语句
EN

Stack Overflow用户
提问于 2018-06-19 07:17:10
回答 1查看 55关注 0票数 -1

下面是我的代码:

代码语言:javascript
复制
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。

我该如何解决这个问题呢?

以下是没有分数移位时的输出示例:

代码语言:javascript
复制
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时:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-06-19 07:39:13

首先,您使用的不是Python 3.x;您使用的是2.7 -您的结果只能在2.7中重现,而不能在3.x中重现。

其次,在这行中有整数除法

代码语言:javascript
复制
weighted_score = int((score_earned / 100) * weight_1)

在Python 2.7中,如果你用一个较小的整型数除以一个较大的整型数,你总是得到一个0。您的行必须是:

代码语言:javascript
复制
weighted_score = int((score_earned / 100.0) * weight_1) # Mind the .0
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50918660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档