Skip to content
Advertisement

Beginner to python or programming as a whole, getting this error and unable to solve it

Here is the code:

p1, p2 = eval(input("input the share price of two organizations:   "))
if (p1>p2):
    print("the first organization is performing better")

here is the error:

TypeError: eval() arg 1 must be a string, bytes or code object

Advertisement

Answer

Python’s eval() allows you to evaluate arbitrary Python expressions from a string-based or compiled-code-based input. However, as the comments correctly say, this is best avoided.

All you need to do from here is convert strings to integers.

This would work better:

# get the inputs:    p1, p2
b = input("input the share price of two organizations:  ")

b = b.split(sep=',') # split the list into 2

# strip the white spaces
p1= b[0].strip()
p2= b[1].strip()

# convert to integers
p1, p2 = int(p1), int(p2)

# do some logic
if (p1>p2):
    print("the first organization is performing better")
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement