indentation error python 1 [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i have no clue why there's an indentation area but it's realy stresssing me out
print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
print "Now %s, are you ready for your password to be tested?" % (name)
if raw_input() = "yes"
print "what is your password?"
if raw_input() = "no"
print "Suit yourself, %s!" % (name)

first of all, your indentation is off when you write your if statements. You need to bring them back by one tab space.
Here is an example of the working code:
print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
print "Now %s, are you ready for your password to be tested?" % (name)
# I fixed the indentation below:
if raw_input() == "yes": #<-- fixed comparison operator here and added colon
print "what is your password?"
if raw_input() == "no": #<-- fixed comparison operator here and added colon
print "Suit yourself, %s!" % (name)
Here I also changed the = sign to ==. The = sign is what you use when you are declaring a variable, like name = raw_input(), which creates the variable name with the value of the raw_input().
The == sign is what you use when you are comparing two things, like raw_input() == "yes", which checks whether or not the raw_input() value is equal to "yes".

A lot of issues with your program, so I would strongly suggest you to go through some tutorials
print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
yes_no = raw_input("Now %s, are you ready for your password to be tested?" % (name))
if yes_no.lower() == 'yes':
print "what is your password?"
if yes_no.lower() == "no":
print "Suit yourself, %s!" % (name)
Always assign your raw_inputs to a variable.
Ex:
Change this print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
to this:
name = raw_input("first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
Secondly
When ever you are using if, for, while etc you have to end the statements with a : and any statements which you want to execute as long as your condition is held true should be indented.
Ex:
if yes_no == 'yes':
print 'Ok'
print 'Ending program'
This way the layout of your program will be a LOT clearer to the user. Look over some tutorials and try them out

Thanks to Josh B. and letsc, I have now completed the code:
print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
yes_no = raw_input("Now %s, are you ready for your password to be tested?" % (name))
if yes_no.lower() == 'yes':
print "what is your password?"
passw = raw_input()
if passw == "password":
print "really? You\'ve got to be kidding me!"
if passw == name:
print "Anybody who knows your name will be able to guess that!"
if passw == dob:
print "Anyone who knows your birthday can guess that!"
if len(passw) < 5:
print "That password may not be long enough, %s" % (name)
if len(passw) > 20:
print "Are you sure you can remeber that?"
else:
print "That\'s a superb password, %s!!" % (name)
if yes_no.lower() == "no":
print "Suit yourself, %s!" % (name)

Related

how do I reset a input in python

so i have this code that basically consists of you asking questions, but i have it so the input answers the question, so you can only ask one question, then you have to reset the whole thing again and again, and i have it to ask you your name first so i want a loop that ignores that.
print("hello,what is your name?")
name = input()
print("hello",name)
while True:
question = input("ask me anything:")
if question == ("what is love"):
print("love is a emotion that makes me uneasy, i'm a inteligence not a human",name)
break
if question == ("i want a dog"):
print("ask your mother, she knows what to do",name)
break
if question == ("what is my name"):
print("your name is",name)
break
Get rid of the breaks, so the loop keeps prompting for new questions. For performance, change the subsequent if tests to elif tests (not strictly necessary, but it avoids rechecking the string if you get a hit early on):
while True:
question = input("ask me anything:")
if question == "what is love":
print("love is a emotion that makes me uneasy, i'm a inteligence not a human",name)
elif question == "i want a dog":
print("ask your mother, she knows what to do",name)
elif question == "what is my name":
print("your name is",name)
Of course, in this specific case, you could avoid the repeated tests by using a dict to perform a lookup, making an arbitrary number of prompts possible without repeated tests:
# Defined once up front
question_map = {
'what is love': "love is a emotion that makes me uneasy, i'm a inteligence not a human",
'i want a dog': 'ask your mother, she knows what to do',
'what is my name': 'your name is',
# Put as many more mappings as you want, code below doesn't change
# and performance remains similar even for a million+ mappings
}
print("hello,what is your name?")
name = input()
print("hello",name)
while True:
question = input("ask me anything:")
try:
print(question_map[question], name)
except KeyError:
# Or check for "quit"/break the loop/alert to unrecognized question/etc.
pass
print("hello,what is your name?")
name = input()
print("hello",name)
while True:
question = input("ask me anything:")
if question == ("what is love"):
print("love is a emotion that makes me uneasy, i'm a inteligence not a human",name)
elif question == ("i want a dog"):
print("ask your mother, she knows what to do",name)
elif question == ("what is my name"):
print("your name is",name)
Take out the breaks. Then have one of the options be "quit" with a break

Why is my if true/elif/else-code not working?

In python, I am trying to make this code accept the user to move forward if he writes "True", and not if he writes "False" for the statement in User_Answer. When I run the code however, I get the "the answer is correct!"-part no matter what I write. The part I am having trouble with starts with "Test_Answer".
Could anyone help me with this?
name_list = ["Dean", "Bill", "John"]
enter_club = ["Enter", "enter"]
print ("THE CLUB - by Mads")
print (" ")
print ("""You approach a secret club called \"The club\". The club members are dangerous.
Make sure you tell the guard one of the members names.""")
print ("")
print ("Good evening. Before you are allowed to enter, we need to check if your name is on our list.")
def enter_the_club():
enter_now = input(" \nPress \"enter\" to enter the club... ")
if (enter_now in enter_club) == True:
print (" ")
print ("But as you enter, you are met with an intelegence test. \n It reads:")
check_name = input("What is your name? ")
def list_check():
if (check_name in name_list) == True:
print("Let me check.. Yes, here you are. Enjoy yourself, %s!" % check_name)
enter_the_club()
elif check_name.isalpha() == False:
print("Haha, nice try %s! Let's hear your real name." % check_name)
list_check()
elif (check_name in name_list) == None:
print ("You will need to give us your name if you want to come in.")
list_check()
else:
print ("I am sorry, but I can not find your name on the list, %s." % check_name)
print ("Are you sure that's your listed name?")
list_check()
list_check()
print ("But as you enter, you are met with an intelegence test.")
print (" ")
print ("It reads:")
Test_Answer = True
def IQtest():
User_Answer = input("Is 18/4 % 3 < 18 True or False? ")
if Test_Answer == User_Answer:
print ("Great, %s, the answer is correct!" % check_name)
else:
print ("You are not allowed to enter before the answer is correct, %s!" % check_name)
IQtest()
IQtest()
True is a boolean constant. What the user enters will be either "True" or "False", both character strings. Also, your elif condition cannot be true. What are you trying to do with three decision branches?
Without changing your code too much ... try this?
Test_Answer = "True"
def IQtest():
User_Answer = input("Is 18/4 % 3 < 18 True or False? ")
if Test_Answer == User_Answer:
print ("Great, %s, the answer is correct!" % check_name)
else:
print ("You are not allowed to enter before the answer is correct, %s!" % check_name)
IQtest()
Note that I've also corrected your "else" syntax.
Also, you have no value for check_name; I assume this is a global variable that you've handled elsewhere.

Is it possible to reuse an 'if' statement?

So I am working on a project and I was wondering if it is possible to reuse an 'if' statement. For example, my code right now is
import re
import string
userinput=''
print "Hello! What is your name?"
userinput = str(raw_input("My name is "))
if not re.search(r'[A-Za-z]', userinput):
print "That isn't a name!"
print str(raw_input("My name is "))
and it prints
Hello! WHat is your name?
My name is 86124674983#
That isn't a name!
My name is 986421674941
986421674941
As you can see, it recognizes anything other than letters as an invalid entrance, but it only does it once. If you input symbols the second time it prompts you for a name, it takes that random input and prints it. I want it to print
Hello! WHat is your name?
My name is 86124674983#
That isn't a name!
My name is 986421674941
That isn't a name!
My name is Eli
Sorry if this confuses anyone. If you need anything clarified don't hesitate to ask. Thanks very much in advance!!
Use a while loop:
print "Hello! What is your name?"
while True:
userinput = raw_input("My name is ")
if not re.search(r'[A-Za-z]', userinput):
print "That isn't a name!"
else:
break
print userinput
Note you don't print a raw_input() - or make it str (it already is). All you need is the raw_input('text') and it will display text.
Use a while-loop:
print "Hello! What is your name?"
while True:
userinput = raw_input("My name is ")
if re.search(r'[A-Za-z]', userinput):
break
print "That isn't a name!"
print userinput

welcome. denied!!! press enter to quit [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 1 year ago.
Improve this question
I tried to create a username and password program for my family, but when I open it it says:
Welcome.
Denied!!!
Press enter to quit.
Can anyone solve my problem? The code is below:
print ("Welcome.")
username = ("Please enter your username.")
if username == ("mom, savanna, joseph"):
print ("Granted.")
password = input ("Please enter your password.")
if password == ("1975, 2000, jesus"):
print ("Granted.")
question = ("Do you like it?")
if question == ("yes"):
print ("I thought you would.")
if question == ("no"):
print ("I guess I could do better")
else:
print ("error")
else:
print ("Denied!!!")
else:
print ("Denied!!!")
input ("press enter to quit")
Your primary issue is on this line:
username = ("Please enter your username.")
I presume you mean to have an input function call there.
username = input("Please enter your username.")
That will solve your immediate rejection issue.
You also have the same issue on this line:
question = ("Do you like it?")
Which should also be:
question = input("Do you like it?")
Here's the thing:
You are new to code. We can see that here. However, there is always room for improvement and you can learn from mistakes. Some people make odd code sometimes ;)
I have created a working model for you to see where you went wrong.
print ("Welcome.")
username = input("Please enter your username.")
if username in ("mom", "savanna", "joseph"):
password = input("Please enter your password.")
if password in ("1975","2000","jesus"):
print ("Granted.")
question = input("Do you like it?")
if question == ("yes"):
print ("I thought you would.")
elif question == ("no"):
print ("I guess I could do better")
else:
print ("error")
else:
print ("Denied!!!")
break
You typed question = ("Do you like it?") without the input. This is important since it actually lets the user type something.
You wrote if username == ("mom,savanna,joseph")
this means that if the username is mom,savanna,joseph then you get in. obviously you wanted only one of these to get in. the Boolean operator or can help a lot in this situation.
Notice how I used elif instead of if. Python can only have one if and one else. but as many elifs as you want. It means else-if.
EDIT:
After reading Joel's comment, I noticed that you probably want Mom's password to be 1975, Savannah's to be 2000, and Joseph's to be jesus. How do you think you can do that by using my code given here?
I'm assuming you want to have 3 different usernames, and mom's password to be 1975 etc.
password = {"mom":"1975", "savanna":"2000", "joseph":"jesus"}
print ("Welcome.")
username = input("Please enter your username.")
if username in password: #if x in dict returns True if dict has the key x
print ("Granted.")
mypassword = input ("Please enter your password.")
if mypassword == password[username]:
print ("Granted.")
answer = input("Do you like it?")
if answer == ("yes"):
print ("I thought you would.")
elif answer == ("no"):
print ("I guess I could do better")
else:
print ("error")
else:
print ("Denied!!!")
else:
print ("Denied!!!")
input("press enter to quit")
You may want to put a loop around it so that it doesn't have to get restarted if there's a bad entry.
you are asking if username == ("mom, savanna, joseph") instead of if username in ("mom, savanna, joseph")
if you entered in "mom" for username when it asked you, it would be like comparing this
if "mom" == ("mom, savanna, joseph"):
("mom, savanna, joseph") and "mom" are not equal
to check if an item is in a list use the in keyword
>>> x = [0, 1, 2, 3, 4]
>>> 3 in x
True
you also did the same thing when checking if password ==
also #Alexander O'Mara is correct, you never did the input() for username

Alphabet check as input

Hi I just start learning python today and get to apply what I learning on a flash cards program, I want to ask the user for their name, and only accept alphabet without numbers or symbols, I've tried several ways but there is something I am missing in my attempts. Here is what I did so far.
yname = raw_input('Your Name ?: ')
if yname.isdigit():
print ('{0}, can\'t be your name!'.format(yname))
print "Please use alphbetic characters only!."
yname = raw_input("Enter your name:?")
print "Welcome %s !" %yname
but I figured in this one is if the user input any character more than one time it will eventually continue...So I did this instead.
yname = raw_input("EnterName").isalpha()
while yname == True:
if yname == yname.isalpha():
print "Welcome %s " %(yname)
else:
if yname == yname.isdigit():
print ("Name must be alphabetical only!")
yname = raw_input('Enter Name:').isalpha()
This while loop goes on forever, as well as I tried (-) and (+) the raw input variable as I've seen in some tutorials. So I thought of using while loop.
name = raw_input("your name"):
while True:
if name > 0 and name.isalpha():
print "Hi %s " %name
elif name < 0 and name.isdigit():
print "Name must be Alphabet characters only!"
try:
name != name.isalpha():
except (ValueError):
print "Something went wrong"
This will check for both alphabet in the raw_input and check for the length of the name as I see you tried to do in your last try.
import string
import re
name = re.compile(r'[a-zA-Z]+') #This will check for alphabet.
yname = raw_input("Your Name:") #ask the user for input.
while not name.match(yname):
print "invalid characters"
yname = raw_input("Your Name:")
if 5<=len(yname)<=10:
print "Hi,", yname, "!"
elif len(yname)>10:
print "too long!"
elif len(yname)<5:
print "too short!"
You can rearrange your last attempt a bit to get what you want:
while True:
name = raw_input("your name") # ask inside the loop
if name and name.isalpha():
print "Hi %s " %name
break # leave the loop if done
elif name and name.isdigit():
print "Name must be Alphabet characters only!"
else:
print "Please enter something"
Note that if name will be True if name != "".
name = raw_input('Enter your name: ')
while not name.isalpha():
print 'Invaid characters in name'
name = raw_input('Enter your name: ')
Use regexes:
import re
regex = re.compile("^[a-zA-Z]+$")
valid_name = False
while not valid_name:
user_name = raw_input("EnterName")
if not regex.search(user_name):
print "this can't be your name"
else:
print "Hi there, {0}".format(user_name)
valid_name = True
Also, please take note that programmers often make false assumptions about human names
Edit: as an alternative you can skip compiling a regex and just use the pattern in place:
if not re.search("^[a-zA-Z]+$", user_name):
...
However, since you're doing it in a loop compiled version would have slightly better performance, since re.search actually compiles a regex behind the scenes each time invoked.
Also, please note I've changed match to search and slightly modified a regex since there're some differences and it appears tome me that search suits your situation more.

Categories