Python Forum
create a function for odd integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create a function for odd integers
#1
So I am trying to create a function for odd integers. I would like to have the user input any number and then have the program test if it is evenly divisible by 2. If so, I would like it to return False and if it is not true I would like to return True. Here is what I have so far:

print("PRACTICE 2.")

number = (input("Enter a number, please: ")

def is_odd(number):
  if number % 2 == 0:
    return False
  else:
    return True
is_odd(number)
Output:
Enter a number, please: 5 (entered by me to test)
Error:
Traceback (most recent call last): File "python", line 13, in <module> File "python", line 9, in is_odd TypeError: not all arguments converted during string formatting
Reply
#2
Quote:File "python", line 13, in <module>

There are only 10 lines in the code you posted. You are posting something differenct from the code you ran to get this error.
Reply
#3
As I said in the other thread, you need to convert number to an integer with int().
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Jan-07-2019, 07:43 PM)ichabod801 Wrote: As I said in the other thread, you need to convert number to an integer with int().

You ignored the last comment back on that thread so I had to make a new one for someone willing to help me.

(Jan-07-2019, 07:07 PM)woooee Wrote:
Quote:File "python", line 13, in <module>

There are only 10 lines in the code you posted. You are posting something differenct from the code you ran to get this error.

Sorry. I have comments in mine that takes up the first three lines so it starts at line four.

# Making a function calling odd numbers
# Using return True and return False

print("PRACTICE FUNCTIONS.")

number = input("Enter a number, please: ")

def is_odd(number):
  if number % 2 == 0:
    return False
  else:
    return True
print(is_odd(number))
Error:
Traceback (most recent call last): File "python", line 13, in <module> File "python", line 9, in is_odd TypeError: not all arguments converted during string formatting
Reply
#5
(Jan-07-2019, 09:29 PM)NLittle17 Wrote: You ignored the last comment back on that thread so I had to make a new one for someone willing to help me.

If you are two impatient to wait more than three hours for an answer, then I'm willing to stop helping you.

Otherwise, yeah, number = int(input("Enter a number, please: "))

And making duplicate threads for the same question is against the rules, so please don't do it again.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,484 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  python create function validation mg24 1 861 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 808 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create a function for writing to SQL data to csv mg24 4 1,206 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 967 Sep-30-2022, 07:45 PM
Last Post: deanhystad
  How to define a function to create a resorted list? sparkt 6 2,860 Aug-08-2020, 04:10 PM
Last Post: sparkt
  Pass integers to datetime.date function florian 3 2,743 Jul-18-2020, 04:43 AM
Last Post: scidam
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,482 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Tried to create a function in a Datacamp course - why a is not equal to x_copy? danlin123 1 1,748 Jun-21-2020, 09:40 PM
Last Post: jefsummers
  create function let_to_num() al_Czervik 2 2,137 Apr-17-2020, 10:44 PM
Last Post: al_Czervik

Forum Jump:

User Panel Messages

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