2016-12-14 6 views
-1

Wenn ich den Code ausführen und ausführen, scheint das Programm den c-Eingang nicht zu speichern, daher wird der Code nicht fortgesetzt, um den Rest der Rechnerfunktion auszuführen.Warum funktioniert mein Python-Rechner nicht?

def calc(): 

    print("Press 1 for addition") 
    print("Press 2 for subtraction") 
    print("Press 3 for multiplication") 
    print("Press 4 for division") 

    c = input() 

    if c == 1: 
     print("Enter a number") 
     x = input() 
     print("Enter another number") 
     y = input() 
     return x + y 

    elif c == 2: 
     print("Enter a number") 
     x = input() 
     print("Enter another number") 
     y = input() 
     return x - y 

    elif c == 3: 
     print("Enter a number") 
     x = input() 
     print("Enter another number") 
     y = input() 
     return x * y 

    elif c == 4: 
     print("Enter a number") 
     x = input() 
     print("Enter another number") 
     y = input() 
     return x/y 

calc() 

Ich habe verbessert jetzt den Code aber kann nicht scheinen, um den Einzug richtig zu machen, und es scheint, dass die Rückkehr-Funktion auf jede Art von Mathematik, die ‚außerhalb Funktion‘ ist

ausgeführt wird
def calc(): 
print("Press 1 for addition") 
print("Press 2 for subtraction") 
print("Press 3 for multiplication") 
print("Press 4 for division") 

c = int(input()) 

def get_inputs(): 
    print("Enter a number") 
    x = int(input()) 
    print("Enter another number") 
    y = int(input()) 
    return x, y 

if c == 1: 
    x, y = get_inputs() 
    return x + y #These return functions seem to be an error 

elif c == 2: 
    x, y = get_inputs() 
    return x - y 

elif c == 3: 
    x, y = get_inputs() 
    return x * y 

elif c == 4: 
    x, y = get_inputs() 
    return x/y 

calc() 
+4

'Eingang()' gibt eine Zeichenfolge in Python 3; Sie wollen eine ganze Zahl 'c = int (Eingabe())' –

+0

Was ist das Problem? – harshil9968

Antwort

0

input() in Python 3 ist das gleiche wie raw_input() in Python 2.7, die Objekt str Typ zurückgibt. Sie müssen explizit auf int-Typ gegossen

c = int(input()) 
+0

Dies behandelt nicht alle Probleme und erklärt nicht, warum es benötigt wird. – Li357

0

Verwenden Sie zuerst c = int(input()) stattdessen die Eingabezeichenfolge zu konvertieren integer. Auch, um nur das Programm sauberer zu machen, sind da Sie bereits Funktionen (calc) verwenden, kann für jede Operation als auch in einer Funktion, um den Eingangsteil setzen:

def get_inputs(): 
    print("Enter a number") 
    x = int(input()) 
    print("Enter another number") 
    y = int(input()) 
    return x, y 

Und dann für jede Operation etwas tun, wie :

if c == 1: 
    a, b = get_inputs() 
    return a + b 

(Edit) versuchen Sie dies:

def get_inputs(): 
    print("Enter a number") 
    x = int(input()) 
    print("Enter another number") 
    y = int(input()) 
    return x, y 

def calc(): 
    print("Press 1 for addition") 
    print("Press 2 for subtraction") 
    print("Press 3 for multiplication") 
    print("Press 4 for division") 

    c = int(input()) 

    if c == 1: 
     x, y = get_inputs() 
     return x + y 

    elif c == 2: 
     x, y = get_inputs() 
     return x - y 

    elif c == 3: 
     x, y = get_inputs() 
     return x * y 

    elif c == 4: 
     x, y = get_inputs() 
     return x/y 

calc() 
+0

Das hat perfekt funktioniert, aber jetzt habe ich Probleme mit der Einrückung. Immer noch neu und gewöhnungsbedürftig. –

+0

Wo haben Sie Probleme mit Einzug? – abacles

+0

Die get_inputs() für jede mathematische Funktion –