int() can't convert non-string with explicit base - python

This is my code:
import easygui
from random import randint
Minimum = easygui.enterbox(msg = "Choose your minimum number")
Maximum = easygui.enterbox(msg = "Choose your maximum number")
operator = easygui.enterbox( msg="which operator would you like to use? X,/,+ or - ?",title="operator")
questions = easygui.enterbox(msg = "enter your desired amount of questions")
for a in range(int(questions)):
rn1 = randint(int(Minimum), int(Maximum))
rn2 = randint(int(Minimum), int(Maximum))
answer = easygui.enterbox("%s %s %s =?" %(rn1, operator, rn2))
realanswer = operator (int(rn1,rn2))
if answer == realanswer:
print "Correct"
else:
print 'Incorrect, the answer was' ,realanswer
When I try and run it, all the enterboxes come up fine, it is when it looks at line 13 that it produces this error:
int() can't convert non-string with explicit base
I tried running the code without the int(), and then it gives me:
'str' object is not callable

First: Your operator is a string, not a function. You can't call '/'(2,3), so if operator=='/', you still can't call operator(2,3).
Second: int(rn1), int(rn2) is how you convert two different numbers to integers, not int(rn1, rn2).
Third: Return values from randint() are already integers, and don't need to be converted again.
I'd suggest converting your numbers to integers as they're entered, only once, rather than doing so on each reference. Thus:
minimum = int(easygui.enterbox(msg="Choose your minimum number"))
maximum = int(easygui.enterbox(msg="Choose your maximum number"))
operator = easygui.enterbox(msg="which operator would you like to use? X,/,+ or - ?", title="operator")
questions = int(easygui.enterbox(msg="enter your desired amount of questions"))
# Select a function associated with the chosen operator
operators = {
'*': lambda a,b: a*b,
'/': lambda a,b: a/b,
'+': lambda a,b: a+b,
'-': lambda a,b: a-b,
}
operator_fn = operators.get(operator)
if operator_fn is None:
raise Exception('Unknown operator %r' % operator)
for a in range(questions):
rn1 = randint(minimum, maximum))
rn2 = randint(minimum, maximum))
answer = int(easygui.enterbox("%s %s %s = ?" % (rn1, operator, rn2)))
realanswer = operator_fn(rn1,rn2)
if answer == realanswer:
print "Correct"
else:
print 'Incorrect, the answer was', realanswer

Your operator variable hold a string. You have to use that string to determine the real operation to perform.
Something like that:
if operator == "+":
realanswer = rn1 + rn2
elif operator == "-":
realanswer = rn1 - rn2
elif operator == "/":
realanswer = rn1 / rn2
elif operator == "*":
realanswer = rn1 * rn2
else
raise Exception('Bad operator {}'.format(operator))
Or better using the operator module:
# top of your program
import operator
my_operators = { '+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.div }
# ...
# and later:
realanswer = my_operators[operator](rn1,rn2)
Of course, in a real application, you would somehow have to deal with "invalid" user input. For example using proper exception handling. But this is an other story...

operator is just a string, you still need to write the code that makes it mean something. You could do something like:
if operator in ('+', 'add'):
realanswer = rn1 + rn2
elif operator in ('-', 'subtract'):
realanswer = rn1 - rn2
else:
print operator, "is not valid"

Related

TypeError: 'int' object is not subscriptable while doing s-expression in Python [duplicate]

This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 11 days ago.
I am trying to write a basic s-expression calculator in Python using s-expression which can contains add or multiply or both or none or just an integar number.
I tried the following snippet:
def calc(expr):
print(expression[0])
if isinstance(expr, int):
return expr
elif expr[0] == '+':
return calc(expr[1]) + calc(expr[2])
elif expr[0] == '*':
return calc(expr[1]) * calc(expr[2])
else:
raise ValueError("Unknown operator: %s" % expr[0])
# Example usage
# expression = ('+', ('*', 3, 4), 5)
expression = (7)
result = calc(expression)
print(result)
When I tried to pass the expression ('+', ('*', 3, 4), 5) , it gives the correct answer but when I just try to use number 7 or 7 inside tuple (7), it gives the above error. How to solve this?
Code is fine, debug is not
You print used to debug is not correctly placed, or assume expression to be a Sequence, not an int.
[Good practice] Don't print a global variable but local: print(expr).
This is less confusing and will help you for debugging this code.
[Branch simplification] Replace every elif with if. Since you return in every branch, you don't need elif. Remove the else too. This will allows you to place code after the first if that will be run for all remaining branches without having to place it in every elif.
def calc(expr: int | tuple):
print(expr)
if isinstance(expr, int):
return expr
if expr[0] == '+':
return calc(expr[1]) + calc(expr[2])
if expr[0] == '*':
return calc(expr[1]) * calc(expr[2])
raise ValueError("Unknown operator: %s" % expr[0])
[Fix] Move the print below the first if.
def calc(expr: int | tuple):
if isinstance(expr, int):
return expr
print(expr)
if expr[0] == '+':
return calc(expr[1]) + calc(expr[2])
if expr[0] == '*':
return calc(expr[1]) * calc(expr[2])
raise ValueError("Unknown operator: %s" % expr[0])
This code works, both for (7) and ('+', ('*', 3, 4), 5).

create a calculator division

I have the below code and when I type calculate(2, 0, "/") I would like to print "Division with zero" than just the output, when I divide with 0. Any suggestions?
def calculate(num1, num2, operator):
if operator == "/" or operator == 'divide':
output = float(num1) / float(num2) if float(num2) else 0
return output
I'd suggest allowing calculate to raise ZeroDivisionError and having the caller print the error:
def calculate(num1, num2, operator):
if operator == "/" or operator == 'divide':
return num1 / num2
try:
print(calculate(2, 0, "/"))
except ZeroDivisionError:
print("Division with zero")
That way you aren't having one function either return or print depending on the situation -- calculate always computes a result (which might include a ZeroDivisionError) without printing it, and the calling code is always in charge of printing it out.
You can simply add an additional condition handling the case you mentioned.
Example:
def calculate(num1, num2, operator):
if num2 == 0 :
print("Division with zero")
return 0
if operator == "/" or operator == 'divide':
output = float(num1) / float(num2)
return output
def main():
calculate(4, 2, "/") # 2
calculate(2, 0, "/") # 0
if __name__ == "__main__":
main()

converting infix to prefix in python

I am trying to write an Infix to Prefix Converter where e.g. I would like to convert this:
1 + ((C + A ) * (B - F))
to something like:
add(1, multiply(add(C, A), subtract(B, F)))
but I get this instead :
multiply(add(1, add(C, A), subtract(B, F)))
This is the code I have so far
postfix = []
temp = []
newTemp = []
def textOperator(s):
if s is '+':
return 'add('
elif s is '-':
return 'subtract('
elif s is '*':
return 'multiply('
else:
return ""
def typeof(s):
if s is '(':
return leftparentheses
elif s is ')':
return rightparentheses
elif s is '+' or s is '-' or s is '*' or s is '%' or s is '/':
return operator
elif s is ' ':
return empty
else :
return operand
infix = "1 + ((C + A ) * (B - F))"
for i in infix :
type = typeof(i)
if type is operand:
newTemp.append(i)
elif type is operator:
postfix.append(textOperator(i))
postfix.append(newTemp.pop())
postfix.append(', ')
elif type is leftparentheses :
newTemp.append(i)
elif type is rightparentheses :
next = newTemp.pop()
while next is not '(':
postfix.append(next)
next = newTemp.pop()
postfix.append(')')
newTemp.append(''.join(postfix))
while len(postfix) > 0 :
postfix.pop()
elif type is empty:
continue
print("newTemp = ", newTemp)
print("postfix = ", postfix)
while len(newTemp) > 0 :
postfix.append(newTemp.pop())
postfix.append(')')
print(''.join(postfix))
Can someone please help me figure out how I would fix this.
What I see, with the parenthetical clauses, is a recursive problem crying out for a recursive solution. The following is a rethink of your program that might give you some ideas of how to restructure it, even if you don't buy into my recursion argument:
import sys
from enum import Enum
class Type(Enum): # This could also be done with individual classes
leftparentheses = 0
rightparentheses = 1
operator = 2
empty = 3
operand = 4
OPERATORS = { # get your data out of your code...
"+": "add",
"-": "subtract",
"*": "multiply",
"%": "modulus",
"/": "divide",
}
def textOperator(string):
if string not in OPERATORS:
sys.exit("Unknown operator: " + string)
return OPERATORS[string]
def typeof(string):
if string == '(':
return Type.leftparentheses
elif string == ')':
return Type.rightparentheses
elif string in OPERATORS:
return Type.operator
elif string == ' ':
return Type.empty
else:
return Type.operand
def process(tokens):
stack = []
while tokens:
token = tokens.pop()
category = typeof(token)
print("token = ", token, " (" + str(category) + ")")
if category == Type.operand:
stack.append(token)
elif category == Type.operator:
stack.append((textOperator(token), stack.pop(), process(tokens)))
elif category == Type.leftparentheses:
stack.append(process(tokens))
elif category == Type.rightparentheses:
return stack.pop()
elif category == Type.empty:
continue
print("stack = ", stack)
return stack.pop()
INFIX = "1 + ((C + A ) * (B - F))"
# pop/append work from right, so reverse, and require a real list
postfix = process(list(INFIX[::-1]))
print(postfix)
The result of this program is a structure like:
('add', '1', ('multiply', ('add', 'C', 'A'), ('subtract', 'B', 'F')))
Which you should be able to post process into the string form you desire (again, recursively...)
PS: type and next are Python built-ins and/or reserved words, don't use them for variable names.
PPS: replace INFIX[::-1] with sys.argv[1][::-1] and you can pass test cases into the program to see what it does with them.
PPPS: like your original, this only handles single digit numbers (or single letter variables), you'll need to provide a better tokenizer than list() to get that working right.

how to "add" things together python

I wrote a function like this, the op gives a operation sign which like '+','-','*','/' or more, the code "adds" everything use the given operator,
Here is the code:
def arithmetic(op,*args):
result = args[0]
for x in args[1:]:
if op =='+':
result += x
elif op == '-':
result -= x
elif op == '*':
result *= x
elif op == '/':
result /= x
return result
Is there a way i can use the +,-,*,/ directly? So I don't have to write an If-Else statement?
You can use the corresponding operators:
import operator
def arithmetic(opname, *args):
op = {'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.div}[opname]
result = args[0]
for x in args[1:]:
result = op(result, x)
return result
or shorter, with reduce:
import operator,functools
def arithmetic(opname, arg0, *args):
op = {'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.div}[opname]
return functools.reduce(op, args, arg0)
I think you're looking for the builtin reduce function combined with operator:
import operator
a = range(10)
reduce(operator.add,a) #45
reduce(operator.sub,a) #-45
reduce(operator.mul,a) #0 -- first element is 0.
reduce(operator.div,a) #0 -- first element is 0.
Of course, if you want to do this using strings, you can map the strings to an operation using a dict:
operations = {'+':operator.add,'-':operator.sub,} # ...
then it becomes:
reduce(operations[your_operator],a)
For the + operator, you have the built-in sum function.
You can use exec:
def arithmetic(op, *args):
result = args[0]
for x in args[1:]:
exec('result ' + op + '= x')
return result

Turn string into operator

How can I turn a string such as "+" into the operator plus?
Use a lookup table:
import operator
ops = { "+": operator.add, "-": operator.sub } # etc.
print(ops["+"](1,1)) # prints 2
import operator
ops = {
'+' : operator.add,
'-' : operator.sub,
'*' : operator.mul,
'/' : operator.truediv, # use operator.div for Python 2
'%' : operator.mod,
'^' : operator.xor,
}
def eval_binary_expr(op1, oper, op2):
op1, op2 = int(op1), int(op2)
return ops[oper](op1, op2)
print(eval_binary_expr(*("1 + 3".split())))
print(eval_binary_expr(*("1 * 3".split())))
print(eval_binary_expr(*("1 % 3".split())))
print(eval_binary_expr(*("1 ^ 3".split())))
How about using a lookup dict, but with lambdas instead of operator library.
op = {'+': lambda x, y: x + y,
'-': lambda x, y: x - y}
Then you can do:
print(op['+'](1,2))
And it will output:
3
You can try using eval(), but it's dangerous if the strings are not coming from you.
Else you might consider creating a dictionary:
ops = {"+": (lambda x,y: x+y), "-": (lambda x,y: x-y)}
etc... and then calling ops['+'] (1,2) or, for user input:
if ops.haskey(userop):
val = ops[userop](userx,usery)
else:
pass #something about wrong operator
There is a magic method corresponding to every operator
OPERATORS = {'+': 'add', '-': 'sub', '*': 'mul', '/': 'div'}
def apply_operator(a, op, b):
method = '__%s__' % OPERATORS[op]
return getattr(b, method)(a)
apply_operator(1, '+', 2)
Use eval() if it is safe (not on servers, etc):
num_1 = 5
num_2 = 10
op = ['+', '-', '*']
result = eval(f'{num_1} {op[0]} {num_2}')
print(result)
Output :
15
I understand that you want to do something like:
5"+"7
where all 3 things would be passed by variables,
so
example:
import operator
#define operators you wanna use
allowed_operators={
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv}
#sample variables
a=5
b=7
string_operator="+"
#sample calculation => a+b
result=allowed_operators[string_operator](a,b)
print(result)
I was bugged with the same problem, using Jupyter Notebook, I was unable to import the operator module. So the above code helped give me insight but was unable to run on the platform. I figured out a somehwhat primitive way to do so with all the basic funcs and here it is: (This could be heavily refined but it’s a start…)
# Define Calculator and fill with input variables
# This example "will not" run if aplha character is use for num1/num2
def calculate_me():
num1 = input("1st number: ")
oper = input("* OR / OR + OR - : ")
num2 = input("2nd number: ")
add2 = int(num1) + int(num2)
mult2 = int(num1) * int(num2)
divd2 = int(num1) / int(num2)
sub2 = int(num1) - int(num2)
# Comparare operator strings
# If input is correct, evaluate operand variables based on operator
if num1.isdigit() and num2.isdigit():
if oper is not "*" or "/" or "+" or "-":
print("No strings or ints for the operator")
else:
pass
if oper is "*":
print(mult2)
elif oper is "/":
print(divd2)
elif oper is "+":
print(add2)
elif oper is "-":
print(sub2)
else:
return print("Try again")
# Call the function
calculate_me()
print()
calculate_me()
print()

Categories

Resources