Debugging code

Writing a program that asks input/integer from a user and determining if its an even or odd number

#asking for an input from the user

try:
    odd_or_even = int(input("Enter an integer: "))
    
#showing error if float or letter or word is entered

except ValueError:
    print (f'the program does not accept a float or letter or word')
    
#determinig the last digit entered 

last_digit = int(repr(odd_or_even)[-1])

#if conditional statement to determine even number

if last_digit  == [0,2,4,6,8]:
    print (f'you have entered an even number')
    
#if not even then its odd

else:
    print (f'you have entered an odd number')

Can someone please help me to debug this, l know there is a simpler way of writing this program but with what l have done please help me to debug this

Thank you,
Jagmeet

Take a look at this article: The Python Modulo Operator - What Does the % Symbol Mean in Python? (Solved)

The modulo operator is a very handy tool to have when programming.

yes l have seen it and found another way to solve it but going by my logic l am trying to find my mistake

What mistake? What is not working as expected and how do you expect it to work?

Determining the last digit entered is not working as l expect

In this line of code it tells me, name cannot be defined, for odd_or_even

so you get this error when you don’t enter a integer:

the program does not accept a float or letter or word
Traceback (most recent call last):
  File "<string>", line 13, in <module>
NameError: name 'odd_or_even' is not defined

is that what your question is about? or something else?

you handle the exception, so the program continues running after the exception has occurred, trying to execute code which is not going to work

yes this is the issue

Normally, when an exception occurs this will give an error and stop the program

handling/catching this exception means the code will continue running.

there are couple of options, you could put all your code in the try block for example. or you could exit the program on exception.

does this make the program work?

can you please write for me the corrected version and an resources on handling exception

thanks

my question has not been answered, someone kindly help

thanks

that is what I do for a living, for that I get paid. On the forum I just help/guide people

You can use google to find plenty of resources, right?

you have not even told me how l have gone wrong, l thought the purpose of the community was to help once you come across challenges

I have:

this code:

last_digit = int(repr(odd_or_even)[-1])

is reached even when odd_or_even contains invalid input. This is problematic

ya, why is this a problem? That is what l am asking

Reverse ask: Why is it not a problem?

you validate something, then regardless of the validation you continue running the code, which very will might result in another exception

I don’t know l am not an expert

If you cannot deal with the query, it will be helpful if you can pass it on to someone else

you can wait for @toastedpitabread to respond, he is also in this topic

# asking for an input from the user

num = int(input("Enter an integer: "))


# determining the last digit entered

#last_digit = num % 10

last_digit = repr(num)[-1]

print ("the last digit is ", last_digit)

#if conditional statement to determine even number

if last_digit == 0 or 2 or 4 or 6 or 8:
    print( str(num), "is an even number")

# if not even then its odd

else :
    print(str(num) ,"is an odd number")