【发布时间】: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