Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-If语句,如果输入是某个字符串_Python_Python 3.x_If Statement_Input - Fatal编程技术网

Python-If语句,如果输入是某个字符串

Python-If语句,如果输入是某个字符串,python,python-3.x,if-statement,input,Python,Python 3.x,If Statement,Input,所以我试图创建一种计算器,它可以处理所有类型的方程。你所要做的就是输入你需要帮助的内容,它会根据你需要帮助的等式问你一系列问题,然后它会返回一个值。我试图使它,以便当一个特定的字符串输入,它会问一系列的问题。然而,不管我输入了什么,它都会问所有的问题。 我正在使用Python 3.6 whichEquation = input("What are you having trouble with? ") if whichEquation: "interest" r = fl

所以我试图创建一种计算器,它可以处理所有类型的方程。你所要做的就是输入你需要帮助的内容,它会根据你需要帮助的等式问你一系列问题,然后它会返回一个值。我试图使它,以便当一个特定的字符串输入,它会问一系列的问题。然而,不管我输入了什么,它都会问所有的问题。 我正在使用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)

那么,如果方程式是斜率或利息,我如何只显示“斜率”方程式或“利息”方程式。

你的缩进不正确,它应该是正确的

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语句,而不仅仅是slope语句


最后,IF语句使用“==”操作符检查一个项是否与某个特定项匹配,该操作符基本上是“等于”,因此
IF whichEquation==“slope”
IF(存储在其中的内容)whichEquation等于“slope”

解决此问题的最短方法是将所有相关代码置于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") 

希望获得此帮助

您可以将代码格式化为:

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)

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

我相信从您所说的内容来看,您希望程序根据所选输入提出问题

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

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

*少于*

*大于>*

小于或等于=

相等于==

不平等=


我建议转到,它演示了不同的IF语句条件。

IF其中的等式:“interest”
->您认为这段代码是什么意思?这同样适用于
如果其中的公式是:“slope”
。我的理解是,如果其中的公式是字符串“interest”,那么它将做某件事。斜率也是一样。这个
如果whichEquation:
意味着“如果whichEquation是真的,即不是空字符串,不是零,等等,那么执行块”,所以在您的情况下,块只包含一个字符串,运行它什么都不做。谢谢,这真的很有帮助!我今天开始学习python,所以谢谢。不用担心!:)Python是一种很棒的语言,我很高兴它对我有所帮助。@SaeedD:那你为什么不看官方教程呢?第四章解释。