Syntax error in Break statement in Python - help needed [closed] - python-3.x

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am beginner in Python so I have been experiencing some issues in loops if python. My concerns involves 'while' loop and 'break' statement.
I have followed as it is mentioned in book and also cross verified from internet but my code does not work still gives me same error.
for i in range(5): j=j+2
print('i:',i, ',j:',j) if j==6:break
Gives me error as below
File "<ipython-input-5-5ff0ac309f49>", line 5
print('i:',i, ',j:',j) if j==6:break
^
SyntaxError: invalid syntax

In python leading spaces in a meaningful. This code works well.
j=0
for i in range(5):
j=j+2
print('i:',i, ',j:',j)
if j==6:
break

You have to use correct indents in python. Since there are no concept of braces, if you want a block to be in a scope, the indents matter. You can refer to this example
Your code in correction will look like:
j = 0
for i in range(5):
j = j + 2
print('i:', i, ',j:', j)
if j == 6:
break
basically, think like every bracket is like a new line+ 4 space indents.

Related

SyntaxError: invalid syntax, I don't know how to fix this code. What's wrong with it? And how to solve it? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
The part of the code that produced the error
def modinverse(a,26):
for x in range(0,26):
if(((a%26)*(b%26))%26 == 1):
return x
return -1
print(modinverse(a,26))
Here is the error I got
File "main.py", line 51
def modinverse(a,26):
^
SyntaxError: invalid syntax
I checked,it looks good. The spellings are fine, brackets and stuff are closed, I don't know what to do. I can't see what's gone wrong.
I ran this on replit.
Maybe def modinverse(a,26) should be something like def modinverse(a,b) or def modinverse(a,b=26):?
In the first case you are declaring a parameter with the name 26.

I migrated from python 2.7 to 3.8.5, now my code won't run. I am showing UnboundLocalError: local variable 'Url_name' referenced before assignment [duplicate]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Python:
I have researched a lot into this, but as a beginner I don't understand the solutions, let alone apply them to my simple problem:
def min_max(xs):
xs = []
for i in xs:
y = (min, max)
return y
"local variable 'y' referenced before assignment"
I don't understand why this does NOT work!!
I've defined Y in the line before return, i know there is another way to do it, but i really need to know why this method is wrong!
Thanks!
assuming that min and max are defined somewhere else....
The loop for i in xs will go around once for each item in xs. Since xs is empty, the loop will not run at all. So, y is never set.

TypeError: 'str' object is not callable (When printing an integer and words) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
#stage 2 dice rolling
#P1D1 means player ones dice one value and so on with P1D2
import random
print("player ones turn")
P1D1 = random.randint (1,6)
print ("your number is "(P1D1))
(this is in python 3.6.3)
when i run this piece of code it returns str object not callable
but when i run
print(P1D1) it seems to print the number without issue
anyone able to help thanks in advance
The error is caused by your syntax on the last line. By putting the brackets right after a string it is like trying to call the string as a function, just like how putting brackets around print, it calls it. Try this.
print ("your number is ", P1D1)

calculation of code is not right and i am not able to find where i make mistake [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've written a program to calculate the standard deviation of a set of numbers. The program is running with no errors, however it is returning an incorrect result.
#standard deviation
import math
def mean(values):
return sum(values)/len(values)
def stanDev(values):
length=len(values)
total_sum = 0
m = mean(values)
for i in range(length):
total_sum += (values[i]-m)**2
under_root=total_sum/(length-1)
return math.sqrt(under_root)
x=[1,2,4,1,2,42,12]
std=stanDev(x)
print(std)
With the current code, I'm getting an output of 3.3243075080628843, however using an online calculator, I'm getting a result of 14.993649449334 for the same set of data.
Do you have the correct indentation in your for loop? The code you supplied should look like this:
length=len(values)
total_sum = 0
m = mean(values)
for i in range(length):
total_sum += (values[i]-m)**2
under_root=total_sum/(length-1) #this line is performed once, after the for loop
return math.sqrt(under_root)

Lexical fault when using ''BOOM!'' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I type this into my texteditor:
boombang xs = [ if x < 10 then ”BOOM!” else ”BANG!”
But when trying to load it, my terminal denies it and and says:
Probably some dumb rookie mistake, but I can't seem to find the solution...:/
There are two problems here.
The first is, you're not using plain ASCII quotation marks (U+0022) ". You're using right quotation marks (U+201D) ”. There's probably a keyboard setting you're using that's causing this, and using certain text editing programs can also prevent this.
Secondly, you seem to be either missing a right bracket. As pointed out, this is probably meant to be part of a list comprehension. You need to write all of the list comprehension for it to work.
The corrected code is:
boombang xs = [if x < 10 then "BOOM!" else "BANG!" | x <- xs]

Resources