EOF problem with ENTER

Prasoon prasoonthegreat at gmail.com
Fri Jun 12 04:58:34 EDT 2009


> 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!!!



More information about the Python-list mailing list