• Register
0 votes
25 views

How can i check if a number is even in python?

I am  new to programming and I was doing some practice. I wrote a code to check if a number is even in python:

number = int(input("Please enter a number to check"))
if (number % ) == 1
    print("the number is even")

but it gives me an error :

  File "main.py", line 2
    if (number % ) == 1
                 ^
SyntaxError: invalid syntax

can anyone help me out to sort out this problem?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes

In your code you have some logic problems . You should correct them. U should use:

if (number % 2) == 0

 And your code should be like:

number = int(input("Please enter a number to check"))
if (number % 2) == 0
    print("{0} the number is even".format(number))

If you use this code you can get accurate result for mgetting even number.

11 6 4
34,950 points

Related questions

1 vote
1 answer 31 views
31 views
Problem: I wrote a code to check if a number is even or odd in python. The program is in the snippet below: number = input("Please enter any number to check if it is even or odd: ") result = number % 2 if result == 0: print("The number ... 2 TypeError: not all arguments converted during string formatting Why this error message is appearing & what is the problem with my program? Thanks in advance.
asked May 13, 2020 samhaz 5k points
0 votes
1 answer 8 views
8 views
I need to check if a number is a perfect squre in java.
asked Oct 5, 2020 Daniel Anderson 4k points
0 votes
1 answer 16 views
16 views asked Oct 29, 2020 john 580 points
1 vote
1 answer 43 views
43 views
Problem: I was trying to check if an element does exist in a list using a pyhton program: primary_list = [ 10, 12, 16, 6, 20, 33 ] a = input("Please enter a number to check if this exists in the list: ") print("Checking if input exists in this list : ") for ... not giving any output that is right. What should I do to fix this and how to check if an element does exist in a list. Thanks in advance.
asked May 16, 2020 samhaz 5k points
1 vote
1 answer 22 views
22 views
Problem: I tried to check if a string is a palindrome or not so I wrote this program in python: def Word(str): for i in range(0, len(str)/2): if str[i] != str[len(str)-i-1]: return False return True a = input("Enter a word to check:") ... TypeError: 'float' object cannot be interpreted as an integer What should I do now and how to check if a string is a palindrome or not using python program?
asked May 15, 2020 samhaz 5k points
3 votes
1 answer 19 views
19 views
Problem: Hi all, I am trying to figure out the dictionary related problems in python. Dictionary is really fun to learn and work with. Let’s say, I have a dictionary dict = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'} If ... needed to check every value inside the dictionary what can I do for that? So, I want to know how to check if a value is in a dictionary python?
asked Mar 24, 2020 Gavin 15.3k points
0 votes
1 answer 22 views
22 views
I am new to python programming I was trying to check if a letter is uppercase in python. I wrote some code to check this statement : import string def UpperCase(s_str): for x in s_str: if x not in string.UpeerCase: return False return True But I can not get the actual ... last): File "<stdin>", line 1, in <module> NameError: name 'LoWWE' is not defined Can anyone help me to solve this thing?
asked Mar 14, 2020 LizzyM 6.1k points
0 votes
1 answer 44 views
44 views
I wrote a code to check if a dictonary is empty or not. I wrote this code in python : def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users) == False: socket.send("No ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) Does my isEmpty returns true and is there any problem in my code.
asked Mar 11, 2020 LizzyM 6.1k points
0 votes
1 answer 23 views
23 views
Can I represent a string as number in python or how can I check if there is any number in string of python function. Supose a function is: def My_num(x): try: float(x) return True except ValueError: return False my question is calling float in the main function is ok or not and I am not getting my expected result.
asked Mar 11, 2020 LizzyM 6.1k points
1 vote
1 answer 23 views
23 views
Problem: I tried to check my dictionares using if the dictionary is in use or not. To check that I wrote something like: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) But the program is not working properly what should I do?
asked Apr 3, 2020 LizzyM 6.1k points