Hello, I am facing problems with my code, I am attaching the code as well as the error output I am receiving

Viewed 61

the code-

def minimum(n):
    L = []
    for i in range(n):
        x = eval(input("enter number 1:"))
        y = eval(input("enter number 2:"))
        if x or y== "\n":
            print("please enter valid value")
        l = [x,y]
        L.append(l)
    print("input keyset-",L)
    for i in range(n):
        if L[i][0] < L[i][1]:
            r = L[i]
            print("FIRST","\t",r)
        elif L[i][0] == L[i][1]:
            r = L[i]
            print("ANY","\t",r)
        else:
            r = L[i]
            print("SECOND","\t",r)

print(minimum(4))

    

I was getting this error-

Traceback (most recent call last):
  File "c:\Users\Ban\Documents\BAANI\code s 1.py", line 22, in <module>
    print(minimum(4))
  File "c:\Users\Ban\Documents\BAANI\code s 1.py", line 5, in minimum
    y = eval(input("enter number 2:"))
  File "<string>", line 0

SyntaxError: unexpected EOF while parsing
3 Answers

you have:

 if x or y== "\n":
       print("please enter valid value")

which always evaluates to True

if x or if y == '\n'

True or False = True --> print (enter valid value)

it'll only evaluate to False if both entries are empty, which is not what you want.

what you want to do is:

 if x =='\n' or y== "\n":
        print("please enter valid value")

also eval(input("enter number 1:")) will throw an exception when sent an empty string '', '\n' or ' ', whech might be causing the EOF error, if after removing it, you are still getting EOF, you want to consider resetting the project or the python file

I've tested the code after the fix it works fine, there's no issue with the logic.

def minimum(n):
    L = []
    for i in range(n):
        x = eval(input("enter number 1:"))
        y = eval(input("enter number 2:"))
        if "\n" in (x, y):
            print("please enter valid value")
        l = [x,y]
        L.append(l)
    print("input keyset-", L)
    for i in range(n):
        if L[i][0] < L[i][1]:
            r = L[i]
            print("FIRST","\t",r)
        elif L[i][0] == L[i][1]:
            r = L[i]
            print("ANY","\t",r)
        else:
            r = L[i]
            print("SECOND","\t",r)

minimum(4)

but there have more efficient way use min lib:

def minimum(n):
    L = []
    for i in range(n):
        x = eval(input("enter number 1:"))
        y = eval(input("enter number 2:"))
        if "\n" in (x, y):
            print("please enter valid value")
        L.append((x, y))
    print("input keyset-", L)
    for pair in L:
        n = min(*pair)
        print(f"the min number: {n}")

minimum(4)

UPDATE: what going on at EOL error

when you try to access user input, you wanna parsing it to int, but python cannot handle it when input is not valid.

x = input("enter number") # x is string now
n = int(x) # n is int

and when you parsing empty string x into int, you will get parsing error, for that reason, you should add error handle

x = input("enter number") # x is string now
try:
  n = int(x) # n is int
except Exception as e:
  # error
  print(str(e))
  continue

do not use eval at this case, no needed, if it is necessary, catch error is the only way to solve it.

I tried running your code and inputting 1, 2, etc... and it worked fine, so the issue is likely caused by your input. However, your code has some issues :

  • In order to convert the string returned by input, you use eval. This is a really bad idea, as what eval does is interpret its argument as Python source code, and return the result. What you want to do is way simpler than that, you just want to convert a string to a number. Instead of eval(input("enter number 1:")), you should use float(input("enter number 1:")). I can't be 100% sure, but I guess that your use of eval is what causes your issue

  • For (I guess) checking that the inputted values are non-empty, you use : if x or y== "\n": This is interpreted as "if x is true or y is a newline character". This is not at all what you want! Since x and y are outputs of eval, you don't know what their type will be, and if using the program correctly, they likely won't be strings, so the comparison to a given string is going to be False (side note: input trims the trailing \n, so when checking for emptiness of an input string, you should use if x == "": or just if not x:)

  • After checking if the input is correct, even if the input is invalid, you go along and use it. You should instead use a while loop

  • Finally, when iterating over L, since you only care about L[i] (and not i per se), you should use for elt in L (elt being the L[i] you used)

With all this, I would rewrite your code to this:

def minimum(n):
    L = []
    for i in range(n):
        while True:
            try:
                x = float(input("Enter number 1:"))
                y = float(input("Enter number 2:"))
            except ValueError:
                print("Please enter valid value")
            else: break
        
        L.append([x, y])
    
    print(f"Input keyset: {L}")
    for elt in L:
        if elt[0] < elt[1]: print("FIRST","\t", elt)
        elif elt[0] == elt[1]: print("ANY","\t", elt)
        else: print("SECOND","\t", elt)

minimum(4)
Related