+ 11

Help on my first code: Even or Odd Machine

I've been trying to write a code in Python that tells if an integer is even or odd. I also want this code to know if the input is an integer or not, so that if the input is a string, it will say 'That is not an integer' instead of an error message. This is the code so far: a = input() #Enter your integer in the input. if int(a) % 2 == 0: print('Your integer ', a, 'is even.') elif int(a) % 2 == 1: print('Your integer', a, 'is odd.') else: print('That is not an integer.') When I enter an integer, it tells if it is even or odd. When I enter a float or string, I still get an error message. Could anyone tell me how I could rephrase the code? An explanation with it would help me learn too. Thanks! EDIT: Solved. Thanks to Toni Isotalo and Pulkit Kamboj for your help.

29th Jun 2018, 5:50 PM
Christine
Christine - avatar
14 Antworten
+ 8
To check if the number is odd should be int(a) % 2 != 0 To check if the input is an integer you could use try and except statements try: a = int(input()) #Enter your integer in the input. if a % 2 == 0: print('Your integer', a, 'is even.') elif a % 2 != 0: print('Your integer', a, 'is odd.') except ValueError: print('That is not an integer')
29th Jun 2018, 6:08 PM
Toni Isotalo
Toni Isotalo - avatar
+ 2
... yes please I've been getting so many notifications
29th Jun 2018, 10:20 PM
Christine
Christine - avatar
+ 1
Help on my first code: Even or Odd Machine for that u have to apply exception handling like this: a = input() #Enter your integer in the input. try: n=int(a) except ValueError: print('this is a string, type an integer') else: if n % 2 == 0: print('Your integer ', a, 'is even.') elif n % 2 == 1: print('Your integer', a, 'is odd.') else: print('this is a floating point number')
29th Jun 2018, 6:09 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
Hello guys I want to scrape movie from website
29th Jun 2018, 9:53 PM
Jatin Mandania
Jatin Mandania - avatar
0
Can anyone help me how to make py for movies pls
29th Jun 2018, 9:54 PM
Jatin Mandania
Jatin Mandania - avatar
29th Jun 2018, 10:11 PM
Jatin Mandania
Jatin Mandania - avatar
0
that the site I want to make py
29th Jun 2018, 10:13 PM
Jatin Mandania
Jatin Mandania - avatar
0
toni
29th Jun 2018, 10:13 PM
Jatin Mandania
Jatin Mandania - avatar
0
ok
29th Jun 2018, 10:14 PM
Jatin Mandania
Jatin Mandania - avatar
0
Is there a way to download on the Samsung phone
29th Jun 2018, 10:18 PM
Jatin Mandania
Jatin Mandania - avatar
0
because I m at work
29th Jun 2018, 10:18 PM
Jatin Mandania
Jatin Mandania - avatar
0
yes
29th Jun 2018, 10:19 PM
Jatin Mandania
Jatin Mandania - avatar
0
I got telegram
29th Jun 2018, 10:20 PM
Jatin Mandania
Jatin Mandania - avatar
0
Or we can chat on messenger
29th Jun 2018, 10:21 PM
Jatin Mandania
Jatin Mandania - avatar