0

Help for odd number check app in phyton

Hello everyone. I'm trying to create a terminal that check the number if it's odd Console prints it's odd number or else prints it's even. I wrote the code like below: num = int(input("Enter your number:")) if num%2 == 0: print("it's even") else: print("it's odd") Here's my problem and question: If user enter invalid value like "W" Console shows the error. I want to do this: if user entered invalid value like "M" Console prints "please enter valid number". Is there any special function for that? *UPDATE*: I wrote the code like below it almost fixed. Only one problem: if user enter a 2 digit number like 25 it shows please enter valid number Fix code: num = input("Enter your number:") numbers = ("1", "2", "3", "4", "5", "6," "7", "8", "9") while True: if num not in numbers: print("enter valid number") num = input("Enter your number:") else: fnum = int(num) if fnum%2 ==0: print("it's even") break else: print("it's odd") break

9th Aug 2023, 2:04 PM
Mohammad Razavi
Mohammad Razavi - avatar
19 Respostas
+ 2
You can use the try/except block in this situation: https://code.sololearn.com/cI4DsxsxrOh7/?ref=app try/except block is a way of catching errors found in the code. The try block is often placed before a code, where it gives exception if something arent right, and the except block is placed after where the exception came(in the try block), which catches the given exception, or leave the specific exception blank to catch all possible exception. Note that except block should be outside the indent of its try block.
10th Aug 2023, 7:39 AM
Dragon RB
Dragon RB - avatar
+ 7
For getting of continue input data you should put this line num = int(input("Enter your number: ")) before while loop as well as at the end of the while loop.
9th Aug 2023, 3:10 PM
JaScript
JaScript - avatar
+ 5
Mohammad Razavi , Since you asked whether to check the input is a valid number... Here using isnumeric () you can check it.... https://code.sololearn.com/c80T0M4NQZ9M/?ref=app
9th Aug 2023, 5:36 PM
Riya
Riya - avatar
+ 4
https://code.sololearn.com/cGu6LXzGXk7K/?ref=app
9th Aug 2023, 2:22 PM
Awab Abdalla Osman
Awab Abdalla Osman - avatar
+ 4
Mohammad Razavi , Because you mentioned clearly in if statement if the entered num not in numbers...print("enter valid number")... the number list consists of single digit numbers....then it will show enter valid number if you enter two or more digit numbers...
9th Aug 2023, 3:02 PM
Riya
Riya - avatar
9th Aug 2023, 3:25 PM
Awab Abdalla Osman
Awab Abdalla Osman - avatar
+ 3
Mohammad Razavi One way to repeatedly prompt for valid input is to use try-except-else inside a while loop, like this: while True: try: num = int(input('Enter your number: ') except ValueError: print('enter valid number') else: break // check odd or even here
9th Aug 2023, 3:32 PM
Mozzy
Mozzy - avatar
+ 2
Awab Abdalla Osman Wow! That was great! But only one question. Your code did great but i want to if any value error happend ask the user to input number again. I used while loop for that but problem is if user ener 2 digit gives value error.
9th Aug 2023, 2:27 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 2
JaScript Thank you🙏
9th Aug 2023, 3:48 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 2
Awab Abdalla Osman Thank you so much! You helped me a lot with your code. 🙌🙏
9th Aug 2023, 3:49 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 2
def check(i,j): if (i==0): return j else: return check(i-1,i+j) print(check(4,7))
11th Aug 2023, 8:36 AM
Sonam
Sonam - avatar
+ 1
Riya Yes i understand. How can i split the numbers from user input digit by digit and check every digit that avalabile in variable like numbers?
9th Aug 2023, 3:48 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 1
Mozzy Thank you 🙏
9th Aug 2023, 3:50 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 1
This is a function for checking if string dose not contain alphabet characters you can use it before converting the string input to integer. https://code.sololearn.com/cWMW0LQi93tD/?ref=app
9th Aug 2023, 4:42 PM
Awab Abdalla Osman
Awab Abdalla Osman - avatar
+ 1
Riya That's exactly what i meant Thank you so much! 👌🙏
9th Aug 2023, 9:36 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 1
Mohammad Razavi Bonus: If you want to check whether something is a number, use: [name].isnumeric(). There are also numbers of function related to the isnumeric(), such as: isalpha() #check if it's written alphabetically, isalnum() #check if it contains both alphabetical and number., And so on. Note that these functions return a boolean value.
10th Aug 2023, 7:48 AM
Dragon RB
Dragon RB - avatar
+ 1
Dragon RB Thanks for your clear and great explanation. I learned lot of things from you 🙏
10th Aug 2023, 11:33 AM
Mohammad Razavi
Mohammad Razavi - avatar
+ 1
Mohammad Razavi You're welcome. I made that code a bit beginner-friendly. I could even make it shorter, but it'd be not beginner-friendly if I do so.
10th Aug 2023, 1:00 PM
Dragon RB
Dragon RB - avatar
0
Awab Abdalla Osman Wow! You're my hero. Thank you brother 🙏
9th Aug 2023, 5:14 PM
Mohammad Razavi
Mohammad Razavi - avatar