#πŸ”’ Basic Practice Problem

32 messages Β· Page 1 of 1 (latest)

violet urchin
#

Here is the problem. I find the wording they used to be a bit confusing:

  1. Write a program that displays all the numbers, greater than one, that divide a number, obtained from the user, evenly (no remainder after division). If the number does not have any even divisors (it is prime), then print "None" instead. Do not include the number itself.

Here is my solution:

Also, just a quick disclaimer I am a beginner programmer so please don't suggest anything too overly complicated that I likely wouldn't have learned yet : ) I appreciate it!

def check_divisors(user_num):
    """
    This function checks if the user entered an 
    even number and displays the numbers if even.

    Parameter: Int - user_num entered by user
    Return Value: None (only printing)
    """ 

    for i in range(2, user_num): 
        if user_num % i == 0: 
            print(user_num)
        else: 
            print("None")


def main(): 

    try: 
        user_num = int(input("Enter a number: "))

    except ValueError: 
        print("Invalid input")
    
    check_divisors(user_num)

if __name__ == "__main__":
    main()
hard gobletBOT
#

@violet urchin

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.

reef panther
#

very confusing wording indeed!

violet urchin
#

agreed

#

too run on of a sentence imo

rare brook
#

they could've just said implement a prime number check πŸ’€

violet urchin
#

fr

reef panther
#

This is a great attempt, and you're almost there, but I notice two issues:

  1. after printing Invalid input, you go on to call check_divisors anyway - consider returning from the function instead
  2. You print None multiple times - once for every possible factor which ends up not being a factor. You should only print None once, if there are no factors
violet urchin
violet urchin
#

Not sure how I can implement that though

reef panther
#

One solution is to count the number of factors using a variable, then after the entire for loop, add an if factor_count == 0: block

#
factor_count = 0

for possible factor in range():
  if its a factor:
    factor_count += 1

if factor_count == 0:
  print("None")```
violet urchin
#

Oh I see !

reef panther
#

There are other ways to go about it, but this is how I would do it as a human so this is how it makes sense to do it with python :)

violet urchin
#

Like this?

reef panther
violet urchin
#

Yup!

reef panther
#

You may want to print out the factor, instead of the number itself

#

i.e. print(i)

violet urchin
#

Wait

#

no you are right

#

they want me to print the numbers that can be divided by user's number

#

Sorry this wording is just bad T_T

#

It worked though! Thanks for the help πŸ™‚

reef panther
#

np

hard gobletBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.