简体   繁体   中英

Need help making this Calculator in Python3

import operator
num1 = float(input("Enter first number: "))
operation = ("")

ops = {
    "+" : operator.add,
    "_" : operator.add,
    "*" : operator.mul,
    "/" : operator.div
}


while operation != ops:
    operation = input("Enter operator: ")
    print("false entry")

num2 = float(input("Enter second number:"))

result = (float(num1) + ops + float(num2))
print(result)

I'm trying to make the operator, if the input is not a operator, repeat and ask to retype is until it is equal to one of the operations listed in the dictionary.

I only started coding like 4 days ago and don't really know what the problem is. I'd be happy if anyone could help me.

Nice try. There are a few problems here...

In ops, you should change the second key/value pair to include subtraction, maybe you are looking for something like this:

ops = {
    "+" : operator.add,
    "-" : operator.sub,
    "*" : operator.mul,
    "/" : operator.div
}

When asking for the operator, you must check if operator is in ops keys. You are now checking if operator is equal to ops dictionary. As pointed out by @Ayam It could be something like this:

operation = input("Enter operator: ")
while operation not in ops:
    print("false entry")
    operation = input("Enter operator: ")

Finally, there is a problem when making the calculation. Once you have the operation and you are sure it is in ops keys, you can use the value (in this case, a function) linked to that key:

result = ops[operation](num1, num2)
print('result is', result)

Hope this helps: Keep learning! :)

You are close: Try this:

operation = input("Enter operator: ")
 while operation not in ops:
   print("false entry")
   operation = input("Enter operator: ")

What goes wrong in your code is that the operation variable is compared to the dictionary called ops. By checking 'operator in ops' you check if operator corresponds to one of the keys of ops.

try this:

   import operator
num1 = float(input("Enter first number: "))
operation = ("")

ops = {
    "+" : operator.add,
    "_" : operator.add,
    "*" : operator.mul,
    "/" : operator.truediv
}

while operation not in ops:
    operation = input("Enter operator: ")
    if(operation not in ops):
        print("false entry")

num2 = float(input("Enter second number:"))

result = (ops.get(operation)(float(num1),float(num2)))
print(result)

Few changes:

  1. while operation:= ops: is comparing a string to a whole dict so extract the keys out first and then compare

    while operation not in ops:

  2. You only print "False entry" if operation is not valid so:

    if(operation not in ops): print("false entry")

  3. Why are you adding the dict and the 2 values here

    result = (float(num1) + ops + float(num2))

You want to use the operator function. The value part of the dict so you have to extract it with dict.get(key). Also the operator functions requires both the argument so do this:

    `result = (ops.get(operation)(float(num1),float(num2)))`

Example: ops.get("+")(2,2) returns operator.add(2,2) = 4

import operator num1 = float(input("Enter first number: "))

ops = {"+": operator.add, "_": operator.add,"*": operator.mul,"/": operator.truediv} operation = input("Enter operator: ") while True: if operation not in ops: print("Wrong input") operation=input("Enter operator: ") else: break num2 = float(input("Enter second number:"))

result = ops[operation](num1, num2) print('result is', result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM