+ 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.

15th Jan 2022, 12:16 AM
Hasan Çakmak
Hasan Çakmak - avatar
9 ответов
+ 4
You just need to set the boolean variable f. If the number is divisible by 2, then f=False otherwise f=True.
15th Jan 2022, 12:52 AM
Solo
Solo - avatar
+ 4
Try adding an else statement in outside loop to print the prime numbers. for: if: print() break else: print()
15th Jan 2022, 1:25 AM
Simba
Simba - avatar
+ 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".
15th Jan 2022, 2:01 AM
Emerson Prado
Emerson Prado - avatar
+ 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))
15th Jan 2022, 2:09 AM
Emerson Prado
Emerson Prado - avatar
+ 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.
15th Jan 2022, 2:37 AM
Solo
Solo - avatar
+ 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.
15th Jan 2022, 3:02 AM
Emerson Prado
Emerson Prado - avatar
+ 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"
15th Jan 2022, 3:19 AM
Solo
Solo - avatar
0
Simba I actually know i could break or i could just return a value but it will stop from giving me all dividers
15th Jan 2022, 1:28 AM
Hasan Çakmak
Hasan Çakmak - avatar
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
15th Jan 2022, 1:34 AM
Hasan Çakmak
Hasan Çakmak - avatar