2016-03-25 7 views
2

Ich versuchte, ein Programm in Python zu schreiben, das (wahrscheinlich schrecklich ineffizient, aber ich schweife) mit mehreren verschiedenen Operationen berechnet. Es gab jedoch einen Fehler, den ich beim Ausführen nicht herausfinden kann. Ich denke, es erfordert, den Typ einer Variablen zu definieren. Das Programm:Fehler im Python-Rechner Ich machte

import math 
print('Select a number.') 
y = input() 
print('Select another number.') 
x = input() 
print('Select what operation you wish to perform. (e for exponentiation, d for division, m for multiplication, a for addition, s for subtraction, mo for modulo, l for log (the base is the first number you entered), r for root)') 
z = input() 
if z == 'e' or z == 'E': 
    print('The answer is ' + y**x) 
elif z == 'd' or z == 'D': 
    print('The answer is ' + y/z) 
elif z == 'm' or z == 'M': 
    print('The answer is ' + y*x) 
elif z == 'a' or z == 'A': 
    print('The answer is ' + y+x) 
elif z == 's' or z == 'S': 
    print('The answer is ' + y-x) 
elif z == 'mo' or z == 'Mo': 
    print('The answer is ' + y%x) 
elif z == 'l' or z == 'L': 
    print('The answer is ' + math.log(x,y)) 
elif z == 'r' or z == 'R': 
    print('The answer is ' + y**(1/x)) 

Der Fehler, der in der Schale auftauchten:

Traceback (most recent call last): 
    File "C:/Users/UserNameOmitted/Downloads/Desktop/Python/Calculator.py", line 7, in <module> 
    z = input() 
    File "<string>", line 1, in <module> 
NameError: name 'd' is not defined 
+0

Verwenden Sie 'raw_input' anstelle von' input'. – Lafexlos

+1

Sind Sie sicher? Diese Zeilennummer ist seltsam - Der Fehler, den ich [online] (https://repl.it/languages/python3) erhalte, ist "Traceback (letzter Aufruf zuletzt): Datei" python ", Zeile 11, in TypeError: nicht unterstützte Operandentypen für /: 'str' und 'str' ". Was schlägt vor, dass dies eine Art von http://stackoverflow.com/questions/24412000/python-type-error-unsupported-operand – usr2564301

Antwort

1

Sie haben mehrere Probleme hier:

  1. z sollte mit raw_input eingegeben werden, nicht input.
  2. In der Division Fall teilen Sie y/z statt y/x.
+0

OP verwendet Python2.7. Also gibt 'input' ein' int' – itzMEonTV

+0

@itzmeontv arg, rechts. Das war dumm von mir. – Mureinik

1

Sie haben

z = raw_input() 

auch Eingang kehrt als int in python2.x .So verwenden zu tun raw_input zu lesen Sie als str Dann überall wie print('The answer is ' + y**x) in print('The answer is ' + str(y**x))

+0

Was ist mit für math.log (x, y)? Würde das zu str (math.log (x, y)) werden? – Mathime

+0

Auch dies ergibt 16/5 = 3. – Mathime

+0

Wenn Sie wollen, Dezimalantwort, tun 'von __future__ Import Division am Anfang der Datei – itzMEonTV

1

Ihr Code hat Fehler

  1. Verwendung raw_input() String-Eingang statt Eingang()
  2. Verwendung int (raw_input()) zu nehmen Integer-Eingang, es ist nicht um einen Fehler zu nehmen, aber es ist eine gute Übung.
  3. Sie versucht, Int mit Zeichenfolge zu teilen.
  4. Sie haben versucht, String und Integer zu verketten.

Ihr Code sollte so etwas sein.

import math 
print('Select a number.') 
y = int(raw_input()) 
print('Select another number.') 
x = int(raw_input()) 
print('Select what operation you wish to perform. (e for exponentiation, d for division, m for multiplication, a for addition, s for subtraction, mo for modulo, l for log (the base is the first number you entered), r for root)') 
z = raw_input() 
if z == 'e' or z == 'E': 
    print('The answer is %d' %(y**x)) 
elif z == 'd' or z == 'D': 
    print('The answer is %d' %(y/x)) 
elif z == 'm' or z == 'M': 
    print('The answer is %d' %(y*x)) 
elif z == 'a' or z == 'A': 
    print('The answer is %d' %(y+x)) 
elif z == 's' or z == 'S': 
    print('The answer is %d' %(y-x)) 
elif z == 'mo' or z == 'Mo': 
    print('The answer is %d' %(y%x)) 
elif z == 'l' or z == 'L': 
    print('The answer is %d' %(math.log(x,y))) 
elif z == 'r' or z == 'R': 
    print('The answer is %d' %(y**(1/x)))