Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use if with and function?
#1
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?
Reply
#2
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).
Reply
#3
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
Reply
#4
Format
try
    input number
    if even number
      do something
    else 
        not even number
        do something
except
    not number
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
See this post
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#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


Forum Jump:

User Panel Messages

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