2017-02-11 4 views
1

Also ich versuche, eine Art Taschenrechner zu erstellen, der alle Arten von Gleichungen behandelt. Alles, was Sie tun müssen, ist einzugeben, worauf Sie Hilfe brauchen, und es wird Ihnen eine Reihe von Fragen stellen, basierend darauf, welche Gleichung Sie benötigen, und es wird einen Wert zurückgeben. Ich versuche es so zu machen, dass wenn eine bestimmte Zeichenfolge eingegeben wird, eine bestimmte Reihe von Fragen gestellt wird. Es fragt jedoch alle Fragen unabhängig davon, was ich eingegeben habe. Ich benutze Python 3.6.Python - Wenn Anweisung, wenn Eingabe ist eine bestimmte Zeichenfolge

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 wie würde ich zeige nur entweder die ‚Neigung‘ Gleichung oder die ‚Interesse‘ Gleichung, wenn whichEquation Neigung oder Interesse ist.

+0

'if whichEquation:" interest "' -> Was denkst du, was dieser Code bedeutet? Dasselbe gilt für 'if whichEquation:" slope "'. – ForceBru

+0

Mein Verständnis war es bedeutet, dass wenn whichEquation die Zeichenfolge "Interesse" ist, dann wird es eine bestimmte Sache tun. Dasselbe gilt für die Steigung. –

+0

Das 'if whereEquation:' bedeutet 'wenn whichEquation ist _truthy_, d. H. Keine leere Zeichenfolge, nicht Null usw., dann führe den Block aus', also in deinem Fall besteht der Block aus einer bloßen Zeichenkette und läuft nichts. – ForceBru

Antwort

2

Ihr Einzug ist falsch, sollte es

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) 

sein, weil alles, was unter einer if-Anweisung eingerückt kommt, ist die Aktion der if-Anweisung tut.

Dies gilt für beide IF-Anweisungen, nicht nur für die Steigung.

Und schließlich, eine Anweisung prüft, ob, wenn ein Element etwas Bestimmtes mit dem „==“ Operator übereinstimmt, die im Grunde „ist gleich“, so ist if whichEquation == "slope" die gleiche wie if (what ever is stored in) whichEquation is equal to "slope"

+0

Danke, das hat wirklich geholfen! Ich habe heute mit Python angefangen, danke. –

+0

Keine Sorge! :) Python ist eine großartige Sprache, ich bin froh, dass es geholfen hat. – ocelot

+0

@SaeedD: Warum liest du nicht das offizielle Tutorial? Kapitel 4 erklärt ['if' Statements] (https://docs.python.org/3/tutorial/controlflow.html#if-statements). – Matthias

0

der kürzeste Weg zu lösen kann dies alle damit verbundenen Code unter

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

Hoffnung diese Hilfe

+0

Ja, es hat geholfen, danke! –

0

Sie formatieren, wie Sie den Code so, wenn Block zu bringen:

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) 

Auf diese Weise Ihre Leerzeichen korrekt ist und jede Bedingung

+0

Danke! Das hat funktioniert. –

0

ich glaube, richtig lesen, was Sie sagen, Sie wollen das Programm Fragen über die gewählten Eingang je fragen.

Um dies zu erreichen, müssen Sie == hinzufügen, um zu prüfen, ob die beiden Variablen gleich sind.

Dies ist, weil Python mehrere Möglichkeiten zum Testen von Variablen mit der If-Anweisung war. Einig gemeinsam diejenigen, die mehr Mathe Zusammenhang sind:

* Weniger als < *

* Größer als> *

weniger als oder gleich < =

Größer als oder gleich> =

Equals ==

Ungleich! =

Ich schlage vor, gehen This python 3 doc, die IF-Anweisung Bedingungen unterschiedlich demonstriert.

Verwandte Themen