1

So I am trying to create a sort of calculator that handles all types of equations. All you have to do is input what you need help on and it will ask you a series of questions based on what equation you need help one, and it will return a value. I am trying to make it so that when a certain string is inputted, it will ask a certain series of questions. However, it asks all the questions regardless of what I input. I'm using 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)

So how would I only show either the 'slope' equation or the 'interest' equation if whichEquation is slope or interest.

3
  • if whichEquation: "interest" -> what do you think this code means? The same goes for if whichEquation: "slope". Commented Feb 11, 2017 at 19:15
  • My understanding was it means that if whichEquation is the string "interest" then it will do a certain thing. Same thing for slope. Commented Feb 11, 2017 at 19:18
  • This if whichEquation: means 'if whichEquation is truthy, i.e. not an empty string, not zero, etc, then execute the block', so in your case, the block consists of a mere string, and running it does nothing. Commented Feb 11, 2017 at 19:33

4 Answers 4

2

Your indentation is incorrect, it should be

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)

This is because anything that comes indented underneath an if statement is the action the if statement does.

This goes for both IF statements, not just the slope one.

And lastly, an IF statement checks if an item matches something specific using the "==" operator, which is basically "is equal to", so if whichEquation == "slope" is the same as if (what ever is stored in) whichEquation is equal to "slope"

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this really helped! I started python today so thanks.
No worries! :) Python is a great language, I'm glad it helped.
@SaeedD: So why don't you read the official tutorial? Chapter 4 explains if statements.
0

the shortest way to solve this is to bring all related code under if block

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") 

hope this help

Comments

0

You can format your code like so:

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)

This way, your whitespace is correct and will properly read each condition

Comments

0

I believe from what you are saying, you want the program to ask questions depending on the selected input.

To accomplish this, you must add == to check if the two variables are equal.

if whichEquation == "slope":

This is because python was multiple ways of testing variables with the if statment. Some common ones that are more math related are:

*Less than < *

*Greater than > *

Less than or equal <=

Greater than or equal >=

Equals ==

Not equal !=

I suggest going to This python 3 doc, which demonstrates different IF statement conditions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.