Also muss ich einen Rechner, der Strings in Floats konvertiert dann berechnen. Das Problem ist, ich brauche, um Fehlermeldungen zu machen, wenn:Erstellen von Fehlermeldungen auf einem Taschenrechner in Python
- der Benutzer eine Zeichenfolge eingibt, der nicht enthält Operanden und/oder einen Operator.
- Der Benutzer gibt nichts ein (der Benutzer drückte einfach die Eingabetaste).
- der Benutzer gibt nur einen Operanden ein.
- gibt der Benutzer nur einen Operator ein.
- Der Benutzer gibt zwei Operanden ein.
- Der Benutzer gibt einen Operanden und einen Operator ein.
- der Benutzer versucht, eine Zahl durch 0 zu teilen.
- der Benutzer hat keinen Abstand zwischen den Operanden und dem Operator.
Dies ist, wie der Code wie Nachrichten ohne Fehler dieses
# Error for not having a space
if (equation.find(" ") == False):
print ("Error #1: Please check if there is a space in between the two operands and the operator.")
# Error for dividing by 0
if (operand2 == "0"):
print ("Error #7: You cannot divide by 0.")
jedoch Python diese So
# Interface
print ("Equation Calculator")
print (" ")
print ("My Equation Calculator is able to")
print (" Add: +")
print (" Subtract: -")
print (" Multiply: *")
print (" Divide: /")
print (" ")
print ("The equation you enter must follow this syntax:")
print (" <openrand><speace><operator><space><operand>.")
print ("An <operand> is any float number.")
print ("An <operator> is any is any of the operators mentioned above.")
print ("A <space> is an empty space.")
# Enter the equation
equation = input ("Enter your equation: ")
# Split the equation into Operand 1,2 and Operator
operand1,operator,operand2 = equation.split(" ")
# Show the user the equation
print ("Here is the equation you have entered: " + equation)
# Addition, Converting strings (operand 1 and 2) into float
if (operator == "+"):
answer = float(operand1) + float(operand2)
# Subtraction, Converting strings (operand 1 and 2) into float
if (operator == "-"):
answer = float(operand1) - float(operand2)
# Multiplication, Converting strings (operand 1 and 2) into float
if (operator == "*"):
answer = float(operand1) * float(operand2)
# DIvision, Converting strings (operand 1 and 2) into float
if (operator == "/"):
answer = float(operand1)/float(operand2)
# Display the answer
print ("The answer is: ",answer)
für 7 und 8. Fehler, die ich tat sieht einfach umgeht und immer noch abstürzt. Was ist das Problem mit dem obigen Code? Wie kann ich es so machen, dass der Code in den obigen 8 Situationen Fehlermeldungen ausgibt? Auch kann ich nicht die integrierten Funktionen eval() oder exec(), brechen oder weitermachen oder übergeben oder sys.exit(). Ich bin sehr neu in der Programmierung im Allgemeinen. Bitte helfen und danke.
Verwenden Sie 'else' Anweisungen – Julien
Sie können versuchen:' 'operand1, operator, operand2 = equation.split (" ")' 'außer ValueError:' um sicherzustellen, dass Sie 3 Strings in 'equation' erhalten.Sie können einen ähnlichen Vorgang ausführen, um 'ValueError' zu fangen, um sicherzustellen, dass die Operanden-Strings in Floats umgewandelt werden können:' try: '' operand1 = float (operand1) '' außer ValueError: '. Weitere Informationen finden Sie unter [Ausnahmen] (https://docs.python.org/3/tutorial/errors.html#exceptions) im Lernprogramm. –