Use if with and function? - Printable Version +- Python Forum ( https://python-forum.io)+-- Forum: Python Coding ( https://python-forum.io/forum-7.html)+--- Forum: General Coding Help ( https://python-forum.io/forum-8.html)+--- Thread: Use if with and function? ( /thread-27237.html) |

Use if with and function? - ibutun - May-30-2020
Hi members, i am new here and I have so many requests in phyton to learn... Please help me... i write a code like this but not working num = input("Please enter a number: ") if num.isalpha(): print(f"{num} is not a number.") elif type(num) == int and (num %2) == 0: #If the value entered is a number and divided by two, print(f"{num} çift sayıdır") #say it as an even number else: print(f"{num} is an odd number")why this code not working... if i enter 4 , it says is an odd number... (this is false) if i enter 5, it says is an odd number... if i enter a, it says is not a number... how do i fix it? RE: Use if with and function? - ndc85430 - May-30-2020
`input` returns a string. You need to convert the value to an int, with the `int` function. Note that that function will throw an exception if the value can't be converted to a number (in base 19 by default).
RE: Use if with and function? - ibutun - May-30-2020
i wrote like this; num = int(input("Please enter a number: ")) if num %2 == 0: #If the value entered is a number and divided by two, print(f"{num} is an even number") #say it as an even number else: print(f"{num} is an odd number")but if some one make a mistake and enter a string , i want to warm RE: Use if with and function? - menator01 - May-30-2020
Format try input number if even number do something else not even number do something except not number RE: Use if with and function? - pyzyx3qwerty - May-30-2020
See this post RE: Use if with and function? - ibutun - May-30-2020
(May-30-2020, 05:56 PM)menator01 Wrote: Format Thank you very much for helping friend. This was my first program :) And i write correct code here for help others num = input("Enter a number: ") try: num = int(num) if num %2 == 0: print(f"{num} is an even number") else: print(f"{num} is an odd number") except ValueError: print(f"{num} isn't a number. Please enter a number!") (May-30-2020, 05:57 PM)pyzyx3qwerty Wrote: See this post This is awesome bro... Not i add while to my code and when i enter "str", my code start over again... İ am very happy today yuppiiiii :D Last edited version: while True: num = input("Enter a number: ") try: num = int(num) if num %2 == 0: print(f"{num} is an even number") else: print(f"{num} is an odd number") break except ValueError: print(f"{num} isn't a number. Please enter a number!") |