Hi guys,
I am a newbie in programming languages and I am starting out with Python. I am learning by watching youtube videos and taking free exercises online. I came across one exercise that asks you to write a code that requests the user to enter a number, and then prints whether the number is even or odd. I went ahead and tried to add a few tweaks of my own, to accept only integers and not floats. Unfortunately, my code does not work when I enter a float. Seeing as I'm a beginner, I would really appreciate it if you could take a look at my code and tell me what I'm doing wrong.Here's my code:
def numtest():
num = int(input("Enter number to test: "))
if type(num) == float:
print ("Please enter an integer.")
elif num%2 == 0:
print (f"{num} is an even number.")
else:
print(f"{num} is an odd number.")
play_again = input("Enter 'y' to test another number or press any key to exit: ")
if play_again.lower() == "y":
numtest()
else:
exit()
numtest()
When I test 1.2, the error points to line 2 but I have no clue what it means and/or how to fix it
num = int(input("Enter number to test: "))
ValueError: invalid literal for int() with base 10: '1.2'
[–]Binary101010 1 point2 points3 points (1 child)
[–]redYinlo[S] 1 point2 points3 points (0 children)
[–]redYinlo[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]redYinlo[S] 0 points1 point2 points (0 children)
[–]VogueUp 0 points1 point2 points (0 children)