Why do I get an invalid syntax error on the “i” in the for loop (python) [closed]
I am learning to program with Python. Currently, I am trying to write a simple function that will check to see if a number that the user has entered is prime. The following portion of the function is throwing up an "invalid syntax" sign:
else:
For x in range(1,b**(1/2)):
If b%x==0:
print(str(b) + " is not prime")
break
"b" is a variable defined elsewhere in the function. It is an integer less than 500 that the user provides. I am getting an "invalid syntax" error pointing at the "x" in the "for x in range..." part of the code. I don't understand why this is the case and would greatly appreciate some insight into this problem.
If it helps, so far, the entire code looks as follows:
def check_prime():
b = input("Enter an integer less than 500: ")
if type(b)!='int':
print("Please enter an integer")
elif b > 500:
print ("Please choose a smaller number")
else:
For x in range(1,b**(1/2)):
If b%x==0:
print(str(b) + " is not prime")
break
print (str(b)+ " is prime")
I am aware that the function will not work exactly as I intend just yet, but I do not know how to move forward until I resolve the issue of the syntax error. Thank you, in advance, for your help.
python python-3.x for-loop syntax
closed as off-topic by timgeb, Yunnosch, stovfl, roganjosh, gnat Nov 20 '18 at 21:18
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – timgeb, Yunnosch, stovfl, roganjosh
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am learning to program with Python. Currently, I am trying to write a simple function that will check to see if a number that the user has entered is prime. The following portion of the function is throwing up an "invalid syntax" sign:
else:
For x in range(1,b**(1/2)):
If b%x==0:
print(str(b) + " is not prime")
break
"b" is a variable defined elsewhere in the function. It is an integer less than 500 that the user provides. I am getting an "invalid syntax" error pointing at the "x" in the "for x in range..." part of the code. I don't understand why this is the case and would greatly appreciate some insight into this problem.
If it helps, so far, the entire code looks as follows:
def check_prime():
b = input("Enter an integer less than 500: ")
if type(b)!='int':
print("Please enter an integer")
elif b > 500:
print ("Please choose a smaller number")
else:
For x in range(1,b**(1/2)):
If b%x==0:
print(str(b) + " is not prime")
break
print (str(b)+ " is prime")
I am aware that the function will not work exactly as I intend just yet, but I do not know how to move forward until I resolve the issue of the syntax error. Thank you, in advance, for your help.
python python-3.x for-loop syntax
closed as off-topic by timgeb, Yunnosch, stovfl, roganjosh, gnat Nov 20 '18 at 21:18
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – timgeb, Yunnosch, stovfl, roganjosh
If this question can be reworded to fit the rules in the help center, please edit the question.
Usefor
andif
instead ofFor
andIf
– Filip Młynarski
Nov 20 '18 at 19:08
For
andIf
should be lower-case
– JETM
Nov 20 '18 at 19:08
1
Also,type(b)
will never return'int'
. It may returnint
, however (Though in Python 3input
always returns a string).
– Patrick Haugh
Nov 20 '18 at 19:08
And b will always be string since that's what input function always returns
– Filip Młynarski
Nov 20 '18 at 19:09
Well I can't believe that I overlooked that. The change from upper to lower case sorted everything out. Also,@PatrickHaugh and @Filip Młynarski , the points about the input function returning a string are duly noted. You have saved me some trouble down the line. You all have my thanks.
– Alex Stephanson
Nov 20 '18 at 19:19
add a comment |
I am learning to program with Python. Currently, I am trying to write a simple function that will check to see if a number that the user has entered is prime. The following portion of the function is throwing up an "invalid syntax" sign:
else:
For x in range(1,b**(1/2)):
If b%x==0:
print(str(b) + " is not prime")
break
"b" is a variable defined elsewhere in the function. It is an integer less than 500 that the user provides. I am getting an "invalid syntax" error pointing at the "x" in the "for x in range..." part of the code. I don't understand why this is the case and would greatly appreciate some insight into this problem.
If it helps, so far, the entire code looks as follows:
def check_prime():
b = input("Enter an integer less than 500: ")
if type(b)!='int':
print("Please enter an integer")
elif b > 500:
print ("Please choose a smaller number")
else:
For x in range(1,b**(1/2)):
If b%x==0:
print(str(b) + " is not prime")
break
print (str(b)+ " is prime")
I am aware that the function will not work exactly as I intend just yet, but I do not know how to move forward until I resolve the issue of the syntax error. Thank you, in advance, for your help.
python python-3.x for-loop syntax
I am learning to program with Python. Currently, I am trying to write a simple function that will check to see if a number that the user has entered is prime. The following portion of the function is throwing up an "invalid syntax" sign:
else:
For x in range(1,b**(1/2)):
If b%x==0:
print(str(b) + " is not prime")
break
"b" is a variable defined elsewhere in the function. It is an integer less than 500 that the user provides. I am getting an "invalid syntax" error pointing at the "x" in the "for x in range..." part of the code. I don't understand why this is the case and would greatly appreciate some insight into this problem.
If it helps, so far, the entire code looks as follows:
def check_prime():
b = input("Enter an integer less than 500: ")
if type(b)!='int':
print("Please enter an integer")
elif b > 500:
print ("Please choose a smaller number")
else:
For x in range(1,b**(1/2)):
If b%x==0:
print(str(b) + " is not prime")
break
print (str(b)+ " is prime")
I am aware that the function will not work exactly as I intend just yet, but I do not know how to move forward until I resolve the issue of the syntax error. Thank you, in advance, for your help.
python python-3.x for-loop syntax
python python-3.x for-loop syntax
asked Nov 20 '18 at 19:06
Alex StephansonAlex Stephanson
1
1
closed as off-topic by timgeb, Yunnosch, stovfl, roganjosh, gnat Nov 20 '18 at 21:18
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – timgeb, Yunnosch, stovfl, roganjosh
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by timgeb, Yunnosch, stovfl, roganjosh, gnat Nov 20 '18 at 21:18
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – timgeb, Yunnosch, stovfl, roganjosh
If this question can be reworded to fit the rules in the help center, please edit the question.
Usefor
andif
instead ofFor
andIf
– Filip Młynarski
Nov 20 '18 at 19:08
For
andIf
should be lower-case
– JETM
Nov 20 '18 at 19:08
1
Also,type(b)
will never return'int'
. It may returnint
, however (Though in Python 3input
always returns a string).
– Patrick Haugh
Nov 20 '18 at 19:08
And b will always be string since that's what input function always returns
– Filip Młynarski
Nov 20 '18 at 19:09
Well I can't believe that I overlooked that. The change from upper to lower case sorted everything out. Also,@PatrickHaugh and @Filip Młynarski , the points about the input function returning a string are duly noted. You have saved me some trouble down the line. You all have my thanks.
– Alex Stephanson
Nov 20 '18 at 19:19
add a comment |
Usefor
andif
instead ofFor
andIf
– Filip Młynarski
Nov 20 '18 at 19:08
For
andIf
should be lower-case
– JETM
Nov 20 '18 at 19:08
1
Also,type(b)
will never return'int'
. It may returnint
, however (Though in Python 3input
always returns a string).
– Patrick Haugh
Nov 20 '18 at 19:08
And b will always be string since that's what input function always returns
– Filip Młynarski
Nov 20 '18 at 19:09
Well I can't believe that I overlooked that. The change from upper to lower case sorted everything out. Also,@PatrickHaugh and @Filip Młynarski , the points about the input function returning a string are duly noted. You have saved me some trouble down the line. You all have my thanks.
– Alex Stephanson
Nov 20 '18 at 19:19
Use
for
and if
instead of For
and If
– Filip Młynarski
Nov 20 '18 at 19:08
Use
for
and if
instead of For
and If
– Filip Młynarski
Nov 20 '18 at 19:08
For
and If
should be lower-case– JETM
Nov 20 '18 at 19:08
For
and If
should be lower-case– JETM
Nov 20 '18 at 19:08
1
1
Also,
type(b)
will never return 'int'
. It may return int
, however (Though in Python 3 input
always returns a string).– Patrick Haugh
Nov 20 '18 at 19:08
Also,
type(b)
will never return 'int'
. It may return int
, however (Though in Python 3 input
always returns a string).– Patrick Haugh
Nov 20 '18 at 19:08
And b will always be string since that's what input function always returns
– Filip Młynarski
Nov 20 '18 at 19:09
And b will always be string since that's what input function always returns
– Filip Młynarski
Nov 20 '18 at 19:09
Well I can't believe that I overlooked that. The change from upper to lower case sorted everything out. Also,@PatrickHaugh and @Filip Młynarski , the points about the input function returning a string are duly noted. You have saved me some trouble down the line. You all have my thanks.
– Alex Stephanson
Nov 20 '18 at 19:19
Well I can't believe that I overlooked that. The change from upper to lower case sorted everything out. Also,@PatrickHaugh and @Filip Młynarski , the points about the input function returning a string are duly noted. You have saved me some trouble down the line. You all have my thanks.
– Alex Stephanson
Nov 20 '18 at 19:19
add a comment |
2 Answers
2
active
oldest
votes
Use for
and if
instead of For
and IF
. additionally, Python will give you an error if you skip the indentation
add a comment |
'For' and 'If' should be 'for' and 'if'. Also, range takes two integers not a int and float.
b=9
for x in range(1,int(b**(1/2))):
if b%x==0:
print(str(b) + " is not prime")
break
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use for
and if
instead of For
and IF
. additionally, Python will give you an error if you skip the indentation
add a comment |
Use for
and if
instead of For
and IF
. additionally, Python will give you an error if you skip the indentation
add a comment |
Use for
and if
instead of For
and IF
. additionally, Python will give you an error if you skip the indentation
Use for
and if
instead of For
and IF
. additionally, Python will give you an error if you skip the indentation
answered Nov 20 '18 at 19:28
Ahmed AbdelreheemAhmed Abdelreheem
212
212
add a comment |
add a comment |
'For' and 'If' should be 'for' and 'if'. Also, range takes two integers not a int and float.
b=9
for x in range(1,int(b**(1/2))):
if b%x==0:
print(str(b) + " is not prime")
break
add a comment |
'For' and 'If' should be 'for' and 'if'. Also, range takes two integers not a int and float.
b=9
for x in range(1,int(b**(1/2))):
if b%x==0:
print(str(b) + " is not prime")
break
add a comment |
'For' and 'If' should be 'for' and 'if'. Also, range takes two integers not a int and float.
b=9
for x in range(1,int(b**(1/2))):
if b%x==0:
print(str(b) + " is not prime")
break
'For' and 'If' should be 'for' and 'if'. Also, range takes two integers not a int and float.
b=9
for x in range(1,int(b**(1/2))):
if b%x==0:
print(str(b) + " is not prime")
break
answered Nov 20 '18 at 19:26
Victor 'Chris' CabralVictor 'Chris' Cabral
1,5201222
1,5201222
add a comment |
add a comment |
Use
for
andif
instead ofFor
andIf
– Filip Młynarski
Nov 20 '18 at 19:08
For
andIf
should be lower-case– JETM
Nov 20 '18 at 19:08
1
Also,
type(b)
will never return'int'
. It may returnint
, however (Though in Python 3input
always returns a string).– Patrick Haugh
Nov 20 '18 at 19:08
And b will always be string since that's what input function always returns
– Filip Młynarski
Nov 20 '18 at 19:09
Well I can't believe that I overlooked that. The change from upper to lower case sorted everything out. Also,@PatrickHaugh and @Filip Młynarski , the points about the input function returning a string are duly noted. You have saved me some trouble down the line. You all have my thanks.
– Alex Stephanson
Nov 20 '18 at 19:19