EOF problem with ENTER

Discussion in 'Python' started by Prasoon, Jun 12, 2009.

  1. Prasoon

    Prasoon Guest

    I am new to python....
    I have written the following program in python.It is the solution of
    problem ETF in SPOJ.....


    #Euler Totient Function

    from math import sqrt
    def etf(n):
    i,res =2,n
    while(i*i<=n):
    if(n%i==0):
    res-=res/i
    while(n%i==0):
    n/=i
    i+=1
    if(n>1):
    res-=res/n
    return res

    def main():
    t=input()
    while(t):
    x=input()
    print str(etf(x))
    t-=1

    if __name__ == "__main__":
    main()


    The problem with my code is that whenever I press an extra "Enter"
    button instead of getting the cursor moved to the next line.....I get
    an error

    _SyntaxError- EOF while parsing and the program terminates.........._

    How should the code be modified so that even after pressing an extra
    "Enter" button the cursor get moved to the next line instead to
    throwing an exception......

    Prasoon
    Prasoon, Jun 12, 2009
    #1
    1. Advertising

  2. Prasoon

    Chris Rebert Guest

    On Thu, Jun 11, 2009 at 11:17 PM, Prasoon<> wrote:
    > I am new to python....
    > I have written the following program in python.It is the solution of
    > problem ETF in SPOJ.....
    >
    >
    > #Euler Totient Function
    >
    > from math import sqrt
    > def etf(n):
    >   i,res =2,n
    >   while(i*i<=n):
    >      if(n%i==0):
    >            res-=res/i
    >      while(n%i==0):
    >            n/=i
    >      i+=1
    >   if(n>1):
    >        res-=res/n
    >   return res
    >
    > def main():
    >  t=input()
    >  while(t):
    >    x=input()
    >    print str(etf(x))
    >    t-=1
    >
    > if __name__ == "__main__":
    >  main()
    >
    >
    > The problem with my code is that whenever I press an extra "Enter"
    > button instead of getting the cursor moved to the next line.....I get
    > an error
    >
    > _SyntaxError- EOF while parsing and the program terminates.........._
    >
    > How should  the code be modified so that even after  pressing an extra
    > "Enter" button the cursor get moved to the next line instead to
    > throwing an exception......


    Use raw_input() instead of input() [at least until you switch to Python 3.x].
    input() does an implicit eval() of the keyboard input, which is (in
    part) causing your problem.
    Note that you'll need to explicitly convert the string raw_input()
    reads in using either int() or float() as appropriate.

    Still, you can't just enter extra lines and expect the program to
    automatically ignore them. You'll have to write the extra code
    yourself to handle empty input from the user.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Jun 12, 2009
    #2
    1. Advertising

  3. Prasoon

    Prasoon Guest

    On Jun 12, 11:28 am, Chris Rebert <> wrote:
    > On Thu, Jun 11, 2009 at 11:17 PM, Prasoon<> wrote:
    > > I am new to python....
    > > I have written the following program in python.It is the solution of
    > > problem ETF in SPOJ.....

    >
    > > #Euler Totient Function

    >
    > > from math import sqrt
    > > def etf(n):
    > >   i,res =2,n
    > >   while(i*i<=n):
    > >      if(n%i==0):
    > >            res-=res/i
    > >      while(n%i==0):
    > >            n/=i
    > >      i+=1
    > >   if(n>1):
    > >        res-=res/n
    > >   return res

    >
    > > def main():
    > >  t=input()
    > >  while(t):
    > >    x=input()
    > >    print str(etf(x))
    > >    t-=1

    >
    > > if __name__ == "__main__":
    > >  main()

    >
    > > The problem with my code is that whenever I press an extra "Enter"
    > > button instead of getting the cursor moved to the next line.....I get
    > > an error

    >
    > > _SyntaxError- EOF while parsing and the program terminates.........._

    >
    > > How should  the code be modified so that even after  pressing an extra
    > > "Enter" button the cursor get moved to the next line instead to
    > > throwing an exception......

    >
    > Use raw_input() instead of input() [at least until you switch to Python 3..x].
    > input() does an implicit eval() of the keyboard input, which is (in
    > part) causing your problem.
    > Note that you'll need to explicitly convert the string raw_input()
    > reads in using either int() or float() as appropriate.
    >
    > Still, you can't just enter extra lines and expect the program to
    > automatically ignore them. You'll have to write the extra code
    > yourself to handle empty input from the user.
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com


    I am using Python 2.6
    I have modified that code
    def main():
    t=int(raw_input())
    while(t):
    x=input()
    print str(etf(x))
    t-=1

    what should i do to handle new line and space......
    We used to get spaces and newline in C using their ASCII values ...can
    similar things be done here???

    Please write the code snippet(by modifying my code) from which i can
    understand something......!!!!!
    Prasoon, Jun 12, 2009
    #3
  4. Prasoon

    Prasoon Guest

    On Jun 12, 11:28 am, Chris Rebert <> wrote:
    > On Thu, Jun 11, 2009 at 11:17 PM, Prasoon<> wrote:
    > > I am new to python....
    > > I have written the following program in python.It is the solution of
    > > problem ETF in SPOJ.....

    >
    > > #Euler Totient Function

    >
    > > from math import sqrt
    > > def etf(n):
    > >   i,res =2,n
    > >   while(i*i<=n):
    > >      if(n%i==0):
    > >            res-=res/i
    > >      while(n%i==0):
    > >            n/=i
    > >      i+=1
    > >   if(n>1):
    > >        res-=res/n
    > >   return res

    >
    > > def main():
    > >  t=input()
    > >  while(t):
    > >    x=input()
    > >    print str(etf(x))
    > >    t-=1

    >
    > > if __name__ == "__main__":
    > >  main()

    >
    > > The problem with my code is that whenever I press an extra "Enter"
    > > button instead of getting the cursor moved to the next line.....I get
    > > an error

    >
    > > _SyntaxError- EOF while parsing and the program terminates.........._

    >
    > > How should  the code be modified so that even after  pressing an extra
    > > "Enter" button the cursor get moved to the next line instead to
    > > throwing an exception......

    >
    > Use raw_input() instead of input() [at least until you switch to Python 3..x].
    > input() does an implicit eval() of the keyboard input, which is (in
    > part) causing your problem.
    > Note that you'll need to explicitly convert the string raw_input()
    > reads in using either int() or float() as appropriate.
    >
    > Still, you can't just enter extra lines and expect the program to
    > automatically ignore them. You'll have to write the extra code
    > yourself to handle empty input from the user.
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com


    I am using Python 2.6
    I have modified that code
    def main():
    t=int(raw_input())
    while(t):
    x=int(raw_input())
    print str(etf(x))
    t-=1

    what should i do to handle new line and space......
    We used to get spaces and newline in C using their ASCII values ...can
    similar things be done here???

    Please write the code snippet(by modifying my code) from which i can
    understand something......!!!!!
    Prasoon, Jun 12, 2009
    #4
  5. On 12 Giu, 08:49, Prasoon <> wrote:
    > On Jun 12, 11:28 am, Chris Rebert <> wrote:
    >
    >
    >
    >
    >
    > > On Thu, Jun 11, 2009 at 11:17 PM, Prasoon<> wrote:
    > > > I am new to python....
    > > > I have written the following program in python.It is the solution of
    > > > problem ETF in SPOJ.....

    >
    > > > #Euler Totient Function

    >
    > > > from math import sqrt
    > > > def etf(n):
    > > >   i,res =2,n
    > > >   while(i*i<=n):
    > > >      if(n%i==0):
    > > >            res-=res/i
    > > >      while(n%i==0):
    > > >            n/=i
    > > >      i+=1
    > > >   if(n>1):
    > > >        res-=res/n
    > > >   return res

    >
    > > > def main():
    > > >  t=input()
    > > >  while(t):
    > > >    x=input()
    > > >    print str(etf(x))
    > > >    t-=1

    >
    > > > if __name__ == "__main__":
    > > >  main()

    >
    > > > The problem with my code is that whenever I press an extra "Enter"
    > > > button instead of getting the cursor moved to the next line.....I get
    > > > an error

    >
    > > > _SyntaxError- EOF while parsing and the program terminates.........._

    >
    > > > How should  the code be modified so that even after  pressing an extra
    > > > "Enter" button the cursor get moved to the next line instead to
    > > > throwing an exception......

    >
    > > Use raw_input() instead of input() [at least until you switch to Python 3.x].
    > > input() does an implicit eval() of the keyboard input, which is (in
    > > part) causing your problem.
    > > Note that you'll need to explicitly convert the string raw_input()
    > > reads in using either int() or float() as appropriate.

    >
    > > Still, you can't just enter extra lines and expect the program to
    > > automatically ignore them. You'll have to write the extra code
    > > yourself to handle empty input from the user.

    >
    > > Cheers,
    > > Chris
    > > --http://blog.rebertia.com

    >
    > I am using Python 2.6
    > I have modified that code
    > def main():
    >   t=int(raw_input())
    >   while(t):
    >     x=int(raw_input())
    >     print str(etf(x))
    >     t-=1
    >
    > what should i do to handle new line and space......
    > We used to get spaces and newline in C using their ASCII values ...can
    > similar things be done here???
    >
    > Please write the code snippet(by modifying my code) from which i can
    > understand something......!!!!!
    >
    > - Mostra testo citato -


    You could do:

    while True:
    x = raw_input("Enter x=>")
    if x != "" : break # if you just press enter, raw_input returns an
    empty string

    Note that this still leaves out the case when you type something which
    is not a number.
    To cover this case, supposing that you need a float, you could do like
    this (NOT TESTED):

    while True:
    x_str = raw_input("Enter x=>")
    if x_str != "" : # to prevent having the error message on empty
    imput
    try:
    x = float(x_str)
    break # if it gets here the conversion in float was succesful
    except ValueError :
    print "The input '%s' cannot be converted in float" % x_str

    This code exits from the loop only when you supply a string that
    represents a floating number


    Ciao
    -----
    FB
    Francesco Bochicchio, Jun 12, 2009
    #5
  6. Prasoon

    Prasoon Guest


    > You could do:
    >
    > while True:
    >    x = raw_input("Enter x=>")
    >    if x != "" : break # if you just press enter, raw_input returns an
    > empty string
    >
    > Note that this still leaves out the case when you type something which
    > is not a number.
    > To cover this case, supposing that you need a float, you could do like
    > this (NOT TESTED):
    >
    > while True:
    >    x_str = raw_input("Enter x=>")
    >    if x_str != "" : #  to prevent having the error message on empty
    > imput
    >       try:
    >          x = float(x_str)
    >          break # if it gets here the conversion in float was succesful
    >       except ValueError :
    >          print "The input '%s' cannot be converted in float" % x_str
    >
    > This code exits from the loop only when you supply a string that
    > represents a floating number
    >



    I modified my code to

    #Euler Totient Function
    import sys
    from math import sqrt
    def etf(n):
    i,res =2,n
    while(i*i<=n):
    if(n%i==0):
    res-=res/i
    while(n%i==0):
    n/=i
    i+=1
    if(n>1):
    res-=res/n
    return res

    def main():
    while True:
    t=raw_input()
    if t!="":break
    t=int(t)
    while(t):
    while True:
    x=raw_input()
    if x!="":break
    x=int(x)
    print str(etf(x))
    t-=1

    if __name__ == "__main__":
    main()

    Now it is working fine ....thanks!!!
    Prasoon, Jun 12, 2009
    #6
  7. Prasoon wrote:
    > I modified my code to
    >
    > #Euler Totient Function
    > import sys
    > from math import sqrt
    > def etf(n):
    > i,res =2,n
    > while(i*i<=n):
    > if(n%i==0):
    > res-=res/i
    > while(n%i==0):
    > n/=i
    > i+=1
    > if(n>1):
    > res-=res/n
    > return res
    >
    > def main():
    > while True:
    > t=raw_input()
    > if t!="":break
    > t=int(t)
    > while(t):
    > while True:
    > x=raw_input()
    > if x!="":break
    > x=int(x)
    > print str(etf(x))
    > t-=1
    >
    > if __name__ == "__main__":
    > main()
    >
    > Now it is working fine ....thanks!!!
    >

    Hello Prasoon, there's still a major issue with your code, check out
    http://tottinge.blogsome.com/meaningfulnames/
    If your code is a private script targeted at your personal use, well I
    guess you can just ignore my comment. But I'm still impressed by how
    some of the python-list users have dived into your code with such ease.

    Regards,

    Jean-Michel
    Jean-Michel Pichavant, Jun 12, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kobu
    Replies:
    10
    Views:
    607
    Keith Thompson
    Mar 4, 2005
  2. Camellia

    Pass an EOF before pressing enter

    Camellia, Dec 31, 2006, in forum: C Programming
    Replies:
    9
    Views:
    375
    Richard Tobin
    Jan 2, 2007
  3. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    677
    James Kanze
    Jun 15, 2007
  4. Jan Burse
    Replies:
    67
    Views:
    1,029
    Jan Burse
    Mar 14, 2012
  5. HopfZ
    Replies:
    0
    Views:
    537
    HopfZ
    Aug 28, 2006
Loading...

Share This Page