Reputation: 239
So i am making a password system. It ask for the user to enter the password, then check if it's right. I am getting a error with:
%Run HelloPython.py
File "/home/pi/Python Coding/HelloPython.py", line 17
print('Welcome home,', name,)
^
SyntaxError: expected an indented block
Something is wrong. Code:
print('What is your name?')
# Stores everything typed up until ENTER
name = sys.stdin.readline()
print('Hello', name,'Enter password.')
password = sys.stdin.readline()
if password == ("1"):
print('Welcome home,', name,)
else:
print("Password:", password,"Is incorect. Please try again.")
Upvotes: 0
Views: 290
Reputation: 43083
SyntaxError: expected an indented block
Indent your if
-else
statements like below.
==
instead of =
which is an assignment.readline
returns a string, so you should compare it with '1'
string.readline
includes a newline \n
at the end, so call strip()
on it.import sys
print('What is your name?')
# Stores everything typed up until ENTER
name = sys.stdin.readline()
print('Hello', name, 'Enter password.')
password = sys.stdin.readline().strip()
if password == '1':
print("Welcome home,", name)
else:
print("Password:", password, "Is incorrect. Please try again.")
Upvotes: 2
Reputation: 96
So I've re-wrote your code. You're forgetting to indent your if-statements. http://www.secnetix.de/olli/Python/block_indentation.hawk
import sys # Import the 'sys' module
print('What is your name?')
name = sys.stdin.readline()
print('Hello ', name, '. Enter password.')
password = sys.stdin.readline()
# Use '=='
if password == 1:
print("Welcome home, ", name)
# Here you need indentation.
else:
print("Password: ", password," is incorect. Please try again.")
Upvotes: 2
Reputation: 1696
This is not your only error, but it is probably the most easily overlooked:
if password = 1:
What's going on here: 1 is getting stored to the variable password
(Since =
is the storing operator). Then if password
is getting evaluated; variables are truthy in python, so that will evaluate to True
, regardless of what you had stored in password
above.
To remedy this, use ==
for comparing password
, and also since password
is a string, put the 1
in quotes so that it is compared as a string.
if password == "1":
You need to fix your indentation as well, python is dependent on whitespace.
Upvotes: 1