[Tutor] FW: query

Whom Isac wombingsac at gmail.com
Thu Jun 25 06:06:43 CEST 2015


For abhijeet560 at yahoo.in:
I had posted few solution to your question before but unfortunately they
were sent to Alan Gauld mail because I am not used to the tutor at python.org
mail system.
Here is the code that will work:
""ODD/EVEN finder:"""


def odd_or_even():
    X=input("Enter the number which you want to check for odd and even: ")
    try:
        number=int(X)
        print("The number %s is ODD."%(number)if number%2!=0 else "The
number %s is EVEN."%(number))
    except ValueError:
        print("Invalid input")


On Thu, Jun 25, 2015 at 1:59 PM, Whom Isac <wombingsac at gmail.com> wrote:

> *def *odd_or_even():
>     X=input("Enter the number which you want to check for odd and even: ")
>     number=int(X)
>     print("The number %s is ODD."%(number) *if *number%2!=0 *else *"The
> number %s is EVEN."%(number))
>
> On Thu, Jun 25, 2015 at 1:53 PM, Whom Isac <wombingsac at gmail.com> wrote:
>
>> Hi, Abhijeet560 at yahoo.in:
>> there is four or five ways to do your question as I had done one for you
>> before. As you could tell there are also a shorter version to do this,
>> using list comprehension method. I would recommend you to use codeacademy
>> if you are not sure. Here is a quickest way for the ODD/EVEN list
>> comprehension. Both works the same way too.
>>
>>
>> def odd_or_even():
>>     X=input("Enter the number which you want to check for odd and even: ")
>>     number=int(X)
>>     print("The %s is ODD"%(number)if number%2!=0 else "The %s is EVEN"%(number))
>>
>>
>> On Thu, Jun 25, 2015 at 1:47 PM, Whom Isac <wombingsac at gmail.com> wrote:
>>
>>> Yes, I agree with Alan Gauld.
>>>
>>> For Gupta's case:
>>>
>>>  if you wanted to get your point across you should mention your
>>> intention and could have posted any error message along with your code.
>>> Because, your question is vague and if the original script of the code had
>>> been posted, that would have been a huge help.
>>>
>>> And, for Abhijeet560 at yahoo.in:
>>>
>>> Is that any section of the function?? If it is then please, repost your
>>> question with full definition of the function and please read python's rule
>>> to indentation, maybe that's where the error is. However, as you said your
>>> function execute normally, therefore I am assuming you misunderstood how
>>> while loops works. Note for you: I don't think there would be any exception
>>> raise for ValueError in your code so try: and except: method would not be
>>> necessary..
>>>
>>> For a simple odd and even finder I would try to do this:
>>>
>>> """ODD or EVEN Finder: """
>>>
>>> def odd_or_even():
>>>     X=input("Enter the number which you want to check for odd and even: ")
>>>     number=int(X)
>>>     while True:
>>>         if number%2==0:
>>>             print("The number ", number, " is Even.")
>>>             #number +=1
>>>         else:
>>>             print("The number ",number, " is Odd")
>>>         break
>>>     pass
>>>
>>>
>>>
>>>
>>> On Thu, Jun 25, 2015 at 2:27 AM, Alan Gauld <alan.gauld at btinternet.com>
>>> wrote:
>>>
>>>> On 24/06/15 13:58, abhijeet560 at yahoo.in wrote:
>>>>
>>>>> Hey guys can anybody tell me what's wrong with this code: The code is
>>>>> below?
>>>>>
>>>>
>>>> Please in future
>>>> 1) start a new thread with a new post, do not hijack somebody else's
>>>> query. It messes up the archive and threaded mail/newsreaders
>>>>
>>>> 2) Use plain text for posting code, your post is all messed up by the
>>>> mail system so we can't see the code clearly. It is all on one line...
>>>>
>>>>  Actually the point is that when we put "34h4" type of value
>>>>>
>>>> > it's an valueerror but here no handling is been performed
>>>>
>>>> The handling only happens if it occurs inside a try block. It looks as
>>>> if your type conversion (int(...)) happens outside the try block.
>>>> The error is raised by the type conversion.
>>>>
>>>>  while 1:    number=int(input("Enter the number which u want to check
>>>>> for odd and even :"))    try :        if number%2==0:            print("The
>>>>> number",number ," is Even")        else:            print("The number
>>>>> ",number ," is Odd")              except  ValueError:        print("Invalid
>>>>> Input")
>>>>>
>>>>
>>>> Finally, handling an error by simply printing a bland error message
>>>> is usually not a good idea. You effectively hide a lot of valuable
>>>> debugging information. You would be better to just let Python print
>>>> out its usual, much more helpful, error message.
>>>>
>>>> (The exception is where it's the top level of an end-user program
>>>> where the Python trace might scare the users. But that should only
>>>> be after you have thoroughly debugged it and handled most of the
>>>> likely problem scenarios, and hopefully logged the error data
>>>> into a logfile or sent it as an email to your support desk.)
>>>>
>>>>
>>>> --
>>>> Alan G
>>>> Author of the Learn to Program web site
>>>> http://www.alan-g.me.uk/
>>>> http://www.amazon.com/author/alan_gauld
>>>> Follow my photo-blog on Flickr at:
>>>> http://www.flickr.com/photos/alangauldphotos
>>>>
>>>>
>>>> _______________________________________________
>>>> Tutor maillist  -  Tutor at python.org
>>>> To unsubscribe or change subscription options:
>>>> https://mail.python.org/mailman/listinfo/tutor
>>>>
>>>
>>>
>>
>


More information about the Tutor mailing list