【问题标题】:How do I create an if statement requiring a specific range of numbers from input如何创建需要输入中特定数字范围的 if 语句
【发布时间】:2020-03-30 19:54:19
【问题描述】:

在这段代码中,您可以看到我正在创建一个系统,通过检查用户的信用评分,房产的基本价格会发生变化。显然我无法实现折扣价格,因为我无法为信用为 670 到 850 的人创建 if 语句。请提出建议。

# Prompt section for customer
credit_check = '''The listing of this property is currently at $1,000,000.
In order to continue you must participate in a credit check that will evaluate your options of payment.'''
print(credit_check)
answer = input('Would you like to continue on to the credit check?(yes or no) ')

# Results of Prompt
if answer == "yes":
    credit = input('Please enter your credit score ranging 300 through 850: ')
elif answer == "no":
    print('''Unfortunately we can not serve you unless you provide a credit check
    ,thank you. 
    Chandlers Estate Co.''')

# This is the piece of code that I'm stuck on.
if int(credit) ==  :
    print("hi")

【问题讨论】:

  • 对不起,如果我误解了,但检查 credit 是否在 670 和 850 之间的语句将是 670 <= int(credit) <= 850
  • 哇,这就是问题所在。非常感谢!
  • 你能澄清问题是什么吗?

标签: python if-statement input range


【解决方案1】:

你可以用简单数学的方式来写它。

if 650 < credit < 850:
    print("do something")

要排除该范围,只需在 if 后面加上“not”即可。

if not 650 < credit < 850:
    print("do something")

【讨论】:

    【解决方案2】:

    如果您想检查两个数字之间的“信用”,请使用下面的代码。 您可以使用 > 来查看它是否高于 x 并使用 来查看它是否低于 x。 您还可以使用 >= 检查该数字是否为 x 或高于 x

     if credit >= 670 and credit <= 850:
          print("hi")
    

    【讨论】:

    • 您需要将第二个条件更改为credit &lt;= 850,否则您会得到SyntaxError
    猜你喜欢
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多