EOF problem with ENTER
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: EOF problem with ENTER
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..! -- http://mail.python.org/mailman/listinfo/python-list
Re: EOF problem with ENTER
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..! -- http://mail.python.org/mailman/listinfo/python-list
Re: EOF problem with ENTER
> 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!!!
--
http://mail.python.org/mailman/listinfo/python-list
Input problem
I am new to pythonand using python 2.6 I want to know when to use raw_input( ) and when to use input( )??? According to my interpretation one should use input( ) when entering numbers etc and raw_input( ) when a string is too be entered. Correct me if I am wrong Also if I want to enter two numbers 'a' and b such that while entering them through the keyboard there is a space between the two... For example: >>>Enter two numbers: .12 15 Can I use input( ) such that 12 gets accepted in 'a' and 15 in 'b' I mean how to handle spaces???/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Input problem
On Jun 16, 3:34 pm, Francesco Bochicchio wrote: > On 16 Giu, 11:32, Prasoon wrote:> I am new to > pythonand using python 2.6 > > I want to know when to use raw_input( ) and when to use input( )??? > > > According to my interpretation one should use input( ) when entering > > numbers etc and > > raw_input( ) when a string is too be entered. > > > Correct me if I am wrong > > You should almost always use raw_input and write your own code to > validate the > input and convert it. input (wich is roughly equivalent of veval > (raw:_input()) > is officially considered a Bad Choice and as such has been changed in > Python 3.x > ( that is, python 3.x 'input' is equivalent to python 2.x raw_input ). > > P.S : if you are new to python and don't expect to use external > libraries for the next > months (one year?) you might consider to start directly with python > 3.x. > > > Also if I want to enter two numbers 'a' and b such that while entering > > them through the keyboard > > there is a space between the two... > > > For example:>>>Enter two numbers: > > > .12 15 > > > Can I use input( ) such that 12 gets accepted in 'a' and 15 in 'b' > > > I mean how to handle spaces???/ > > For instance: map( int, raw_input.split() ) splits the > input string using blanks as separator, then try to convert each piece > in an integer > and returns a list of integer. Of course if the input string is not a > list of integer > you get an exception. > > You could also do: > > a, b = map( int, raw_input.split() ) > > but in this case you get an exception also if the input strings > cobntains less or more than two integers. > > Ciao > - > FB I think you meant a, b = map( int, raw_input().split() ) Prasoon -- http://mail.python.org/mailman/listinfo/python-list
Re: Input problem
What is the difference between z=int(raw_input()) and z=eval(raw_input())(I thought them to be the same in case of integers) I mean when an integer is entered in that case are they same and when an integer in not entered,in that case how are they different? -- http://mail.python.org/mailman/listinfo/python-list
