#πŸ”’ Can someone help me fill in the numbers?

45 messages Β· Page 1 of 1 (latest)

novel hollow
#

So i got this code here to determine if a number is odd or even, but it takes me to long to code, need help

remote gardenBOT
#

@novel hollow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

novel hollow
#
number = 1842
if number == 0:
    print("Even")
if number == 1:
    print("Odd")
if number == 2:
    print("Even")
if number == 3:
    print("Odd")
if number == 4:
    print("Even")
if number == 5:
    print("Odd")
if number == 6:
    print("Even")
if number == 7:
    print("Odd")
if number == 8:
    print("Even")
if number == 9:
    print("Odd")
if number == 10:
    print("Even")
if number == 11:
    print("Odd")
if number == 12:
    print("Even")
if number == 13:
    print("Odd")
if number == 14:
    print("Even")
if number == 15:
    print("Odd")
if number == 16:
    print("Even")
if number == 17:
    print("Odd")
if number == 18:
    print("Even")
if number == 19:
    print("Odd")
if number == 20:
    print("Even")
if number == 21:
    print("Odd")
if number == 22:
    print("Even")
if number == 23:
    print("Odd")
if number == 24:
    print("Even")
if number == 25:
    print("Odd")
if number == 26:
    print("Even")
if number == 27:
    print("Odd")
if number == 28:
    print("Even")
if number == 29:
    print("Odd")
if number == 30:
    print("Even")
if number == 31:
    print("Odd")
if number == 32:
    print("Even")
if number == 33:
    print("Odd")
if number == 34:
    print("Even")
if number == 35:
    print("Odd")
if number == 36:
    print("Even")
if number == 37:
    print("Odd")
if number == 38:
    print("Even")
if number == 39:
    print("Odd")
if number == 40:
    print("Even")
if number == 41:
    print("Odd")
if number == 42:
    print("Even")
if number == 43:
    print("Odd")
if number == 44:
    print("Even")
if number == 45:
    print("Odd")
if number == 46:
    print("Even")
if number == 47:
    print("Odd")
sweet fox
#

Just to double check: Are you serious or is it a joke ?

novel hollow
#

why wouldnt I?

#

someone told me to use %

#

but i dunno how

sweet fox
#

yep

thorny dock
#

AI could probably help with this

sweet fox
#

% returns the remaining of the integer division

thorny dock
#

Or write a function to write the function

novel hollow
#

ahhh

sweet fox
novel hollow
#

so number % 2 = 0 is even

#

?

#

==*

sweet fox
#

yep

novel hollow
#

ok ty

sweet fox
novel hollow
#

even thought of coding more if clauses with an algorithm

sweet fox
#

repeting the (nearly) same line/block of code usually means you can do it more efficiently

#

At least with a loop and a function, or even with just like a different algorithm

round grotto
livid goblet
sweet fox
#

can you not insult people ?

lyric chasm
#

actually, this code is bad because its only up to 47 numbers and users cant send numbers higher like 500 or something
i would recommend you to do this instead
https://paste.pythondiscord.com/KZWQ

sweet fox
#

it's normal for a beginner to make mistakes, not knowing how to use features and making bad algorithms. That's why you learn

sweet fox
#

or is it a joke ?

lyric chasm
#

isnt that obvious

sweet fox
#

it's not

lyric chasm
#

i wouldnt waste my time writing 1k lines of code, so thats definitely a joke

stuck chasm
#

perhaps people should focus on being helpful or being silent

fair brook
#

Insulting peoples intelligence is not appropriate. if you don't want to give this person the benefit of the doubt, just don't engage with the thread.

livid goblet
silk path
#

there are other ways
like you can use // (integer division)

# checks if rounding down is the same as halfing thus checking if the half is an integer
x//2 == x/2
# same as:
floor(x/2) == x/2
#but floor has to be imported from math

or recursion

# recursion is the fact that a function can call itself, in this case that's done in 2 steps
# as a bonus here you get bot is_even and is_odd 
def is_even(x: int) -> bool:
  if x == 0:
    return True
  return is_odd(x-1)
def is_odd(x: int) -> bool:
  if x == 0:
    return False
  return is_even(x-1)

or loops can do it similarly as you did just for bigger numbers

# your method: counting up and checking if equal
n = 0
is_even = True
while n != x
  is_even = not is_even
  n += 1

or replicate the recursion with loops

# recursion method: counting down checking if 0
is_even = True
while x != 0:
  is_even = not is_even
  x -= 1

there are ways with binary operations

x >> 1 << 1 == x
# or
bool(x & 1) # need bool() because the result is 0 or 1

there are a lot of funny ways
some of them show different tools
but most of them are way slower and more complicated than needed

not x%2 # don't need bool() beacuse not a is the same as not bool(a)

is the most used i think

here is an other joke method

def is_even(x: int) -> bool:
  return not is_odd(x)
def is_odd(x: int) -> bool:
  return not is_even(x)

which is funny because it never finishes and symbolizes people trying to get others to do their tasks resulting in those tasks never getting done

lyric chasm
#
num = input(">> ")
result = "Even" if int(num) % 2 == 0 else "Odd"
print(result)```
how bout this
silk path
lyric chasm
#

oh

#

yea

remote gardenBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.