0
Please correct this code for me TO FIND IF THE NUMBER IS PRIME OR NOT please help
num= int(input("Enter any number:")) f = 0 i = 2 while i <= num/2: if num%i == 0: f=1 break i+=1 if f == 0: print(" it is a Prime Number") else: print(" is Not a Prime Number")
13 odpowiedzi
+ 6
Indentation works like this:
If one line ends with a colon (:), start the next line with four more spaces. Like this:
if condition: #(1)
do_something() #(2)
while condition2: #(3)
print() #(4)
(1) this line ends with a colon (:)
(2) so this line is indented
(3) this line ends with another colon
(4) so this line is even more indented
Two lines that belong together logically, like "if" and the corresponding "else", have to be on the same indentation level:
if condition: #(5)
do_something() #(6)
do_something_2() #(7)
else: #(8)
do_something_else() #(9)
(5) if clause
(6) indented
(7) same indentation level as line before: (6) and (7) belong together and will both be executed if condition (5) is True
(8) "else" belongs to "if" -> needs to be on same indentation level
(9) indented
>> EVERYTHING ELSE is wrong.
This is wrong:
if condition:
do_something()
do_something_2() # indentation error
This is wrong:
if condition:
do_something()
else: # error: not on same level as "if"
do_something_else()
This is wrong:
do_something()
do_something_2() # indentation error, can't change indentation level for no reason
+ 4
Indentation is the most fundamental knowledge of Python
Try putting your code in code playground for our inspection on your indentation
+ 3
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2277/
The explanation is in the yellow box.
When the lines have the same number of space at the beginning of each line, they are considered, for example in C++(i saw you finished C++), in the same code block enclosed by the paired brackets { }.
+ 1
Don't indent line 4
Line 7 should be same indented as line 6
Line 8 should be same indented as line 5
Line 9 shouldn't be indented
Line 11 should be same indented as line 9, in this case not indented
Fix these 5 and your code should work.
Let me know if you don't understand after you corrected your code.
+ 1
Thank you guys for your help my code successfully runs without any error. I am new to python I had done c++ that's why I am suffering this type of error as in c++ we put semicolon and extra brackets. well thanks for your help
0
Ok
0
I know but error is still there can anyone write the code of finding prime number using while loop using python and then send here.
this prime number logic is fully working in c++ but in python I am getting error.
0
You put it in code playground first, and use the + button beside the send button to post the link of your code here.
0
Oh ok
0
Ahmad chitsazzade thanks I corrected it and i got my code without any error.
- 1
no it's not correct
for example enter 1001
Of course, if you put your code in the playground, we could be better able to help, because the code of its distances is meaningless.
- 1
# try it:
import math
def PrimeFunc(number):
if(number <= 0):
print(number, "Is not defined for negative");
elif(number == 1):
print(number, "is not prime");
elif(number == 2):
print(number, "is prime");
for num in range(2,math.ceil(math.sqrt(number)+1)):
if(number % num == 0):
print(number, "is not prime")
return
print(number, "is prime")
# for int input:
number = float
while(number==float):
number = float(input('Enter the +int Number: '))
intnumber = number//1
if number-intnumber==0:
number = int(number)
break
else:
print('***Just Enter the +int Number***')
number = float
PrimeFunc(number)