Is there a way to return an output if there is no output | Sololearn: Learn to code for FREE!

+1

Is there a way to return an output if there is no output

https://code.sololearn.com/cvGXrnZlDvz0/?ref=app I have tried to make an app to show if a number is prime and if it is not it should show what it is divided by. But i couldnt add a line that says it is a prime without it printing on the non prime numbers as well. I was thinking if i could return an output if the first part has no output.

1/15/2022 12:16:17 AM

HASAN ÇAKMAK

9 Answers

New Answer

+4

You just need to set the boolean variable f. If the number is divisible by 2, then f=False otherwise f=True.

+4

Try adding an else statement in outside loop to print the prime numbers. for: if: print() break else: print()

+1

All you need, as Solo already pointed out, is a boolen indicating if the number is prime or not. I just recommend naming this variable something like "is_prime", instead of "f". Variable names without meaning make code unreadable as it grows. Just think in how you should initialize this variable, and how you can use it to conditionally print "this is prime". And yes, the question wording is messy. You don't need an output "if there's no output", but when there's no dividers".

+1

A side thing, also important: don't overcomplicate! You have 5 variables - p, t, y, r and d - with the same information, and a function for a simple routine which is only executed once. Ask yourself: what is really needed? What about this? number = int(input()) for divider in range(2, number): ... print('{} is not prime. {} is a divider.'format(number, divider))

+1

HASAN ÇAKMAK, I repeat once again, you can do it as I advise you, at least in my opinion this is the easiest way, there are many other options. To do this, you need to add only three or four lines of code. Another thing I'm confused about is the last line of your code. Why do you assign the print function to a variable? Just write prime(p), or prime(input()). Emerson Prado, the variable f, or flag, is a common boolean type variable in all programming languages. The variable f is the same as i, n, k and so on.

+1

Solo I see. This becomes confusing when you have multiple flags. Also, "flag" can be many things anyway. Using a real name is a good practice to make sure the reader knows which information is there.

+1

Emerson Prado, the check box will be one. But I'll agree with you only because I completely forgot that Python uses f to form the string: print(f"{y} is not a prime number. {i} is the divider") Then you can safely put flag ⛳ ☺️ Or b, or bln short for "Boolean variable"

0

Simba I actually know i could break or i could just return a value but it will stop from giving me all dividers

0

Solo ohh ok that is smart since all prime numbers are odd except 2 and i just figured i asked the question wrong 😂 i meant i cant print out it is a prime number the code above gives me if a number is not prime and what divides it but i cant an outpur it says the number is prime