首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建if语句时如何创建范围

创建if语句时如何创建范围
EN

Stack Overflow用户
提问于 2020-07-30 21:35:50
回答 1查看 69关注 0票数 0

我对蟒蛇很陌生。我需要帮助处理这段代码:

代码语言:javascript
运行
复制
price = 1000000
credit = int(input("What is your credit score? "))
if credit >= 670:
    print("Since you have a high credit score, you will have to only put down 10% for your new home! ")
    new_price = price/10
    print(f"The price you will have to pay is ${new_price}!")
elif credit <= 670:
    print("Due to your low credit score, you will have to put down 20% for your new home.")
    new_price = price/5
    print(f"The price you will have to pay is ${new_price}!")
elif credit <= 0:
    print("Sorry, that is not a valid credit score!")
elif credit >= 850:
    print("Sorry, that is not a valid credit score!")
else:
    print("Sorry, but you haven't typed in a number! Run the program to try again.")
print("Have a great day!")

所以,我试着用一个非常高的数字运行代码,认为它会导致“对不起,这不是一个有效的信用评分”,但后来我意识到,我在这里没有限制:

代码语言:javascript
运行
复制
if credit >= 670:
    print("Since you have a high credit score, you will have to only put down 10% for your new home! ")
    new_price = price/10
    print(f"The price you will have to pay is ${new_price}!")

因此,它认为任何670分或更高的分数都是高信用分数。是否有代码,或某种方法,我可以使高信用评分至少是670,最高850?

EN

回答 1

Stack Overflow用户

发布于 2020-07-30 21:41:21

您有与670等号的不同条件,如果不是elif状态,您应该与您独占,因为它们实际上是相互排斥的,如下所示:

if credit < 670:

elif 670 <= credit <= 850:

else:

然后,如果希望检查所有条件语句,请使用几个if语句。如果您使用if, elifelse,那么一旦一个大小写匹配,就不会检查其他情况。当只使用if时,将检查所有语句,无论前面是否匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63181712

复制
相关文章

相似问题

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