【问题标题】:Python - If statement if input is a certain stringPython - 如果输入是某个字符串,则如果语句
【发布时间】:2017-06-30 00:37:25
【问题描述】:

所以我正在尝试创建一种可以处理所有类型方程式的计算器。您所要做的就是输入您需要帮助的内容,它会根据您需要帮助的方程式向您提出一系列问题,然后它会返回一个值。我正在尝试这样做,以便在输入某个字符串时,它会提出一系列问题。但是,无论我输入什么,它都会询问所有问题。 我正在使用 Python 3.6。

    whichEquation = input("What are you having trouble with?   ")

if whichEquation:
    "interest"

r = float(input("What is the interest rate?: "))
C = float(input("Deposit cash: "))
t = float(input("For how many years will your deposit be invested?: "))
n = float(input("How many times per year is the interest compounded?: "))

interest = C * (1 + r/n)**(n*t)


print("Your future value is: ",interest,"dollars")

if whichEquation:
    "slope"

y1 = float(input("First y point: "))
y2 = float(input("Second y point: "))
x1 = float(input("First X point: "))
x2 = float(input("Second X point: "))

slope = (y2 - y1)/(x2 - x1)

print("The slope is:",slope)

那么,如果 whichEquation 是斜率或兴趣,我将如何只显示“斜率”方程或“兴趣”方程。

【问题讨论】:

  • if whichEquation: "interest" -> 你认为这段代码是什么意思? if whichEquation: "slope" 也是如此。
  • 我的理解是,如果 whichEquation 是字符串“interest”,那么它会做某事。坡度也是如此。
  • 这个if whichEquation: 的意思是'如果whichEquation 是truthy,即不是空字符串,不是零等,则执行块',所以在你的情况下,块仅由一个字符串组成,运行它什么也不做。

标签: python python-3.x if-statement input


【解决方案1】:

你的缩进不正确,应该是

if whichEquation == "slope":
    y1 = float(input("First y point: "))
    y2 = float(input("Second y point: "))
    x1 = float(input("First X point: "))
    x2 = float(input("Second X point: "))

    slope = (y2 - y1)/(x2 - x1)

    print("The slope is:",slope)

这是因为 if 语句下面缩进的任何内容都是 if 语句执行的操作。

这适用于两个 IF 语句,而不仅仅是斜率语句。

最后,IF 语句使用“==”运算符检查项目是否与特定内容匹配,这基本上是“等于”,因此if whichEquation == "slope"if (what ever is stored in) whichEquation is equal to "slope" 相同

【讨论】:

  • 谢谢,这真的很有帮助!我今天开始使用 python,谢谢。
  • 别担心! :) Python 是一门很棒的语言,我很高兴它有帮助。
  • @SaeedD:那你为什么不阅读官方教程呢?第 4 章解释if statements
【解决方案2】:

解决这个问题的最短方法是将所有相关代码放在 if 块下

if whichEquation == "interest":
    r = float(input("What is the interest rate?: ")) 
    C = float(input("Deposit cash: ")) 
    t = float(input("For how many years will your deposit be invested?: "))
    n = float(input("How many times per year is the interest compounded?: ")) 
    interest = C * (1 + r/n)**(n*t) 
    print("Your future value is: ",interest,"dollars") 

希望对你有所帮助

【讨论】:

    【解决方案3】:

    你可以像这样格式化你的代码:

    whichEquation = input("What are you having trouble with?   ")
    
    if whichEquation == "interest":
    
         r = float(input("What is the interest rate?: "))
         C = float(input("Deposit cash: "))
         t = float(input("For how many years will your deposit be   
         invested?: "))
         n = float(input("How many times per year is the interest compounded?: "))
    
         interest = C * (1 + r/n)**(n*t)
    
    
         print("Your future value is: ",interest,"dollars")
    
     elif whichEquation == "slope":
          y1 = float(input("First y point: "))
          y2 = float(input("Second y point: "))
          x1 = float(input("First X point: "))
          x2 = float(input("Second X point: "))
    
          slope = (y2 - y1)/(x2 - x1)
    
          print("The slope is:",slope)
    

    这样,您的空格是正确的,并且会正确读取每个条件

    【讨论】:

      【解决方案4】:

      我相信从你所说的,你希望程序根据所选输入提出问题。

      为此,您必须添加 == 以检查两个变量是否相等。

      if whichEquation == "slope":
      

      这是因为 python 是使用 if 语句测试变量的多种方法。一些与数学更相关的常见问题是:

      *小于

      *大于 > *

      小于或等于

      大于或等于 >=

      等于 ==

      不等于!=

      我建议去This python 3 doc,它演示了不同的IF语句条件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-26
        • 2014-03-22
        • 2011-01-22
        • 2018-09-29
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多