SyntaxError “break outside loop”
supplied_username = input("Please enter your name. ")
print("Your username has been created and is", supplied_username)
supplied_password = input("Now please create a password. ")
file = open("Login.txt","a")
file.write (supplied_username)
file.write (",")
file.write (supplied_password)
file.write("n")
file.close()
logged_in = False
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
("please, register an account")
i get a syntax error of "breaking outside loop"
why?
im creating a login program which asks the username to register by entering a username and password which gets saved in a text file, then logging in by entering the username and password they just entered to register. if it matches the one saved in the text file, they have successfully logged in, if it doesnt they have to re enter it.
edit: i've fixed it by moving the 'break' 4 spaces forward. but i now get a syntax error of 'unexpected indent'
help please.
python
add a comment |
supplied_username = input("Please enter your name. ")
print("Your username has been created and is", supplied_username)
supplied_password = input("Now please create a password. ")
file = open("Login.txt","a")
file.write (supplied_username)
file.write (",")
file.write (supplied_password)
file.write("n")
file.close()
logged_in = False
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
("please, register an account")
i get a syntax error of "breaking outside loop"
why?
im creating a login program which asks the username to register by entering a username and password which gets saved in a text file, then logging in by entering the username and password they just entered to register. if it matches the one saved in the text file, they have successfully logged in, if it doesnt they have to re enter it.
edit: i've fixed it by moving the 'break' 4 spaces forward. but i now get a syntax error of 'unexpected indent'
help please.
python
3
Indentation is important in python and your break is indeed outside of your for-loop - at least if your code looks exactly like the code you've posted here. The error message should point you to the exact line.
– Mike Scotty
Nov 20 '18 at 9:48
you need 8 spaces not 4 - and not onlybreak
but whole block.
– MrAleister
Nov 20 '18 at 9:49
Break can only be used under a loop.. So if you try to use it under a solo if statement (which is not under any loop) It will always give you an error
– Sandesh34
Nov 20 '18 at 9:50
thankyou. ive fixed it but now i get a syntax error of 'unexpected indent'.. please help! and yes, my code looks exactly like this and i use python 3.4.GUI.
– xxMagnum
Nov 20 '18 at 9:50
I think you should indent your bothif
statements under that for loop
– Sandesh34
Nov 20 '18 at 9:50
add a comment |
supplied_username = input("Please enter your name. ")
print("Your username has been created and is", supplied_username)
supplied_password = input("Now please create a password. ")
file = open("Login.txt","a")
file.write (supplied_username)
file.write (",")
file.write (supplied_password)
file.write("n")
file.close()
logged_in = False
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
("please, register an account")
i get a syntax error of "breaking outside loop"
why?
im creating a login program which asks the username to register by entering a username and password which gets saved in a text file, then logging in by entering the username and password they just entered to register. if it matches the one saved in the text file, they have successfully logged in, if it doesnt they have to re enter it.
edit: i've fixed it by moving the 'break' 4 spaces forward. but i now get a syntax error of 'unexpected indent'
help please.
python
supplied_username = input("Please enter your name. ")
print("Your username has been created and is", supplied_username)
supplied_password = input("Now please create a password. ")
file = open("Login.txt","a")
file.write (supplied_username)
file.write (",")
file.write (supplied_password)
file.write("n")
file.close()
logged_in = False
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
("please, register an account")
i get a syntax error of "breaking outside loop"
why?
im creating a login program which asks the username to register by entering a username and password which gets saved in a text file, then logging in by entering the username and password they just entered to register. if it matches the one saved in the text file, they have successfully logged in, if it doesnt they have to re enter it.
edit: i've fixed it by moving the 'break' 4 spaces forward. but i now get a syntax error of 'unexpected indent'
help please.
python
python
edited Nov 20 '18 at 9:48
xxMagnum
asked Nov 20 '18 at 9:46
xxMagnumxxMagnum
116
116
3
Indentation is important in python and your break is indeed outside of your for-loop - at least if your code looks exactly like the code you've posted here. The error message should point you to the exact line.
– Mike Scotty
Nov 20 '18 at 9:48
you need 8 spaces not 4 - and not onlybreak
but whole block.
– MrAleister
Nov 20 '18 at 9:49
Break can only be used under a loop.. So if you try to use it under a solo if statement (which is not under any loop) It will always give you an error
– Sandesh34
Nov 20 '18 at 9:50
thankyou. ive fixed it but now i get a syntax error of 'unexpected indent'.. please help! and yes, my code looks exactly like this and i use python 3.4.GUI.
– xxMagnum
Nov 20 '18 at 9:50
I think you should indent your bothif
statements under that for loop
– Sandesh34
Nov 20 '18 at 9:50
add a comment |
3
Indentation is important in python and your break is indeed outside of your for-loop - at least if your code looks exactly like the code you've posted here. The error message should point you to the exact line.
– Mike Scotty
Nov 20 '18 at 9:48
you need 8 spaces not 4 - and not onlybreak
but whole block.
– MrAleister
Nov 20 '18 at 9:49
Break can only be used under a loop.. So if you try to use it under a solo if statement (which is not under any loop) It will always give you an error
– Sandesh34
Nov 20 '18 at 9:50
thankyou. ive fixed it but now i get a syntax error of 'unexpected indent'.. please help! and yes, my code looks exactly like this and i use python 3.4.GUI.
– xxMagnum
Nov 20 '18 at 9:50
I think you should indent your bothif
statements under that for loop
– Sandesh34
Nov 20 '18 at 9:50
3
3
Indentation is important in python and your break is indeed outside of your for-loop - at least if your code looks exactly like the code you've posted here. The error message should point you to the exact line.
– Mike Scotty
Nov 20 '18 at 9:48
Indentation is important in python and your break is indeed outside of your for-loop - at least if your code looks exactly like the code you've posted here. The error message should point you to the exact line.
– Mike Scotty
Nov 20 '18 at 9:48
you need 8 spaces not 4 - and not only
break
but whole block.– MrAleister
Nov 20 '18 at 9:49
you need 8 spaces not 4 - and not only
break
but whole block.– MrAleister
Nov 20 '18 at 9:49
Break can only be used under a loop.. So if you try to use it under a solo if statement (which is not under any loop) It will always give you an error
– Sandesh34
Nov 20 '18 at 9:50
Break can only be used under a loop.. So if you try to use it under a solo if statement (which is not under any loop) It will always give you an error
– Sandesh34
Nov 20 '18 at 9:50
thankyou. ive fixed it but now i get a syntax error of 'unexpected indent'.. please help! and yes, my code looks exactly like this and i use python 3.4.GUI.
– xxMagnum
Nov 20 '18 at 9:50
thankyou. ive fixed it but now i get a syntax error of 'unexpected indent'.. please help! and yes, my code looks exactly like this and i use python 3.4.GUI.
– xxMagnum
Nov 20 '18 at 9:50
I think you should indent your both
if
statements under that for loop– Sandesh34
Nov 20 '18 at 9:50
I think you should indent your both
if
statements under that for loop– Sandesh34
Nov 20 '18 at 9:50
add a comment |
1 Answer
1
active
oldest
votes
Try This:
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
supplied_username=supplied_username.strip()
supplied_password=supplied_password.strip('n')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
print("please, register an account")
After resolving problem of Indentation
, the next problem of space.What you store in your file it return it with append space
on it.
So first strip
and then check.
THANK YOU !!!!!
– xxMagnum
Nov 22 '18 at 14:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390201%2fsyntaxerror-break-outside-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try This:
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
supplied_username=supplied_username.strip()
supplied_password=supplied_password.strip('n')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
print("please, register an account")
After resolving problem of Indentation
, the next problem of space.What you store in your file it return it with append space
on it.
So first strip
and then check.
THANK YOU !!!!!
– xxMagnum
Nov 22 '18 at 14:10
add a comment |
Try This:
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
supplied_username=supplied_username.strip()
supplied_password=supplied_password.strip('n')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
print("please, register an account")
After resolving problem of Indentation
, the next problem of space.What you store in your file it return it with append space
on it.
So first strip
and then check.
THANK YOU !!!!!
– xxMagnum
Nov 22 '18 at 14:10
add a comment |
Try This:
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
supplied_username=supplied_username.strip()
supplied_password=supplied_password.strip('n')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
print("please, register an account")
After resolving problem of Indentation
, the next problem of space.What you store in your file it return it with append space
on it.
So first strip
and then check.
Try This:
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
supplied_username=supplied_username.strip()
supplied_password=supplied_password.strip('n')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
print("please, register an account")
After resolving problem of Indentation
, the next problem of space.What you store in your file it return it with append space
on it.
So first strip
and then check.
edited Nov 20 '18 at 10:27
answered Nov 20 '18 at 10:02


Rohit-PandeyRohit-Pandey
987615
987615
THANK YOU !!!!!
– xxMagnum
Nov 22 '18 at 14:10
add a comment |
THANK YOU !!!!!
– xxMagnum
Nov 22 '18 at 14:10
THANK YOU !!!!!
– xxMagnum
Nov 22 '18 at 14:10
THANK YOU !!!!!
– xxMagnum
Nov 22 '18 at 14:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390201%2fsyntaxerror-break-outside-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Indentation is important in python and your break is indeed outside of your for-loop - at least if your code looks exactly like the code you've posted here. The error message should point you to the exact line.
– Mike Scotty
Nov 20 '18 at 9:48
you need 8 spaces not 4 - and not only
break
but whole block.– MrAleister
Nov 20 '18 at 9:49
Break can only be used under a loop.. So if you try to use it under a solo if statement (which is not under any loop) It will always give you an error
– Sandesh34
Nov 20 '18 at 9:50
thankyou. ive fixed it but now i get a syntax error of 'unexpected indent'.. please help! and yes, my code looks exactly like this and i use python 3.4.GUI.
– xxMagnum
Nov 20 '18 at 9:50
I think you should indent your both
if
statements under that for loop– Sandesh34
Nov 20 '18 at 9:50