Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use if with and function?
#6
(May-30-2020, 05:56 PM)menator01 Wrote: Format
try
    input number
    if even number
      do something
    else 
        not even number
        do something
except
    not number

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!")
Reply


Messages In This Thread
Use if with and function? - by ibutun - May-30-2020, 05:42 PM
RE: Use if with and function? - by ndc85430 - May-30-2020, 05:48 PM
RE: Use if with and function? - by ibutun - May-30-2020, 05:55 PM
RE: Use if with and function? - by menator01 - May-30-2020, 05:56 PM
RE: Use if with and function? - by ibutun - May-30-2020, 06:35 PM
RE: Use if with and function? - by pyzyx3qwerty - May-30-2020, 05:57 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020