Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am new to Python
I have a if: else statement.
my problem : right now after print "Welcome to the Script" my script exits , but I want it exits just after else and also when my condition was true continue to line name=raw_input("Enter your name: ") and so on not exit ...
How can I exit from the if else and continue in the script?
script_username="test"
script_password="1"
login_username=raw_input('Please Enter the script username:')
login_password=getpass.getpass('Please Enter the script password:')
if login_password == script_password and login_username == script_username:
print "Welcome to the Script"
else:
print "your Username or Password is false"
exit()
name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")
Try that (see the new indention)
script_username="test"
script_password="1"
login_username=raw_input('Please Enter the script username:')
login_password=getpass.getpass('Please Enter the script password:')
if login_password == script_password and login_username == script_username:
print " Welcome to the Script "
else:
print " your Username or Password is false "
exit()
name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")
I think I found the problem. getpass.getpass('prompt') doesn't return a string. Either fix that or use this:
script_username="test"
script_password="1"
login_username=raw_input('Please Enter the script username:')
login_password=raw_input('Please Enter the script password:')
if login_password == script_password and login_username == script_username:
print " Welcome to the Script "
else:
print " your Username or Password is false "
exit()
name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")
Output:
Please Enter the script username:test
Please Enter the script password:1
Welcome to the Script
Enter your name: Louis
Enter your family: Mom, Dad, Dog
Enter your Sex: Male
hello MaleLouisMom, Dad, Dog
press<enter>
IMPORTANT
This block:
name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")
was indented incorrectly in your question. It should be at the level below (i.e. no spaces) the stuff in the if statement. This would also break your program (because it'd think everything was in the if statement.
EDIT 2: Screenshot:
Related
This code I wrote for my study on if statements in Python doesn't seem to work properly since it asks the same question twice after the first one being asked.
Identity = input("Who are you? ")
if Identity == "Jane":
print("You must be John's sister.")
elif Identity != "John":
print("My database doesn't recognise you. Who are you I wonder...")
elif Identity == "John":
if input("What is your last name? ") != "Travolta":
print("False! John's name is Travolta and not " + input("What is your last name? ") + ".")
elif input("How old are you? ") == "Travolta":
if input("How old are you? ") != "20 yo":
print("False! John is 20 yo and not " + input("How old are you? ") + ".")
elif input("How old are you? ") == "20 yo":
print("You must in fact be John!")
if input("What is your last name? ") != "Travolta":
print("False! John's name is Travolta and not " + input("What is your last name? ") + ".")
Here you aren't storing the value of the input and printing it. You are asking the person to input again "what is your last name?".
Do something like
last_name = input("What is your last name? ")
and use it instead of the input() in the if statement.
Here I am putting the snippet in the way I would've done it.
identity = input("Who are you? ")
if identity == "Jane":
print("You must be John's sister.")
elif identity != "John":
print("My database doesn't recognise you. Who are you I wonder...")
elif identity == "John":
last_name = input("What is your last name? ")
if last_name != "Travolta":
print("False! John's last name is Travolta and not " + last_name + ".")
else:
age = input("How old are you? ")
if age != "20 yo":
print("False! John is 20 yo and not " + age + ".")
else:
print("You must in fact be John!")
It is working properly for every possibility. Examples:
name_list = []
command_list = ["Add" ,"Help"]
def start():
input("Hello please type in a command : ")
start()
if (input == "help" or "Help"):
print("Here are the following commands for this program : ")
i = 0
for i in command_list :
print("'" + i + "'")
start()
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
So this code is causing me a bit of trouble , upon starting the code everything works fine it asks me to type in a command as it should , and if i type in 'help' it does exactly what its supposed to do, which is list all the commands, after that its supposed to reset via the start() command and once again ask me to input a command , however this time no matter what i write it activates this block of code:
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
Can someone please help me fix this and or explain why this is happening??
Thank you in advanced.
You have to use a while loop here, also repr(i) more efficient than "'" + i + "'":
name_list = []
command_list = ["Add" ,"Help"]
def start():
return input("Hello please type in a command : ")
a=start()
while a.lower()=='help':
print("Here are the following commands for this program : ")
i = 0
for i in command_list :
print(repr(i))
a=start()
if a.lower()=='add':
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
else:
print('invalid input')
Example output:
Hello please type in a command : help
Here are the following commands for this program :
'Add'
'Help'
Hello please type in a command : Help
Here are the following commands for this program :
'Add'
'Help'
Hello please type in a command : Add
Insert name please : Bob
Welcome Bob
and also:
print(name_list)
Returns:
['Bob']
Your code has several issues. Here's a version that works.
name_list = []
command_list = ["Add", "Help"]
def start():
return input("Hello please type in a command : ")
name = ""
while not name:
command = start()
if (command.lower() == "add"):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
# printing help for everything except add
else:
print("Here are the following commands for this program : ")
i = 0
for i in command_list:
print(repr(i))
if (input == "help" or "Help"):
print("Here are the following commands for this program : ")
i = 0
for i in command_list :
print("'" + i + "'")
start() # <--- your call to start() function does not reset where the interpreter left off reading you code.
Python interpreter simply reads each one of your lines and does something with it. In your if statement, the line start() is called, it ask for an user input and returns None. Then this if statement is complete, then the next line read is as you described:
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
Solution to your question:
name_list = []
command_list = ["Add" ,"Help"]
def start():
input("Hello please type in a command : ")
while True:
start()
if (input == "help" or "Help"):
print("Here are the following commands for this program : ")
i = 0
for i in command_list :
repr(i)
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
if input == "something for break condition":
break
name_list = []
command_list = ["Add" ,"Help"]
def start():
inpt = input("Hello please type in a command : ")
if (inpt == "help" or inpt == "Help"):
print("Here are the following commands for this program : ")
print(command_list)
start()
elif (inpt == "Add" or inpt == "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
yn = input("Do you want to add another person? ")
if yn in ['y','Y','yes','Yes','YES']:
start()
else:
print ("Quitting!! all the people entered are")
print(name_list)
return (None)
else:
print("Please type 'help' for help")
start()
This question already has answers here:
Allow Only Alpha Characters in Python?
(5 answers)
Closed 6 years ago.
For example a program has the line:
user_input = raw_input(" enter : ")
How do you make it so that the program will only continue once what the user enters are letters only? And is he enter anything else he will get an error.
EDIT: Thanks for the help but it says try again if there is a space. The solution is:
while True:
name = raw_input("Enter name : ")
if name.replace(' ', '').isalpha():
break
else:
print "Try again"
Just use str.isalpha():
user_input = ""
while not user_input.isalpha():
user_input = raw_input(" enter : ")
while True:
user_input = raw_input(" enter: ")
if user_input.isalpha():
break
else:
print "try again"
Original question for context:
I was working on a quick little password program and came across the error:
NameError name 'confirm' is not defined.
I am using Python 3.3, I am trying to use a password then enter no (2) it then supplys me with the error.
Here is my code:
password=input ("Enter a password: ")
print ('Are you sure ' +password+ ' is a safe password?')
print ("1. YES")
print ("2. NO")
confirm=input ("Insert a number: ")
if comfirm is 2:
password=input ("Enter a password: ")
print ('Are you sure ' +password+ ' is a safe password?')
print ("1. YES")
print ("2. NO")
confirm=input ("Insert a number: ")
if confirm is 2:
password=input ("Enter a password: ")
print ('Are you sure ' +password+ ' is a safe password?')
print ("1. YES")
print ("2. NO")
confirm=input ("Insert a number: ")
Edit, a few years later:
Please ensure you always look over your code for spelling and syntaxical mistakes, especially in new lines of code you have just deployed/built/executed. Mistakes can range from spelling mistakes to incorrect case or to syntax and beyond.
Just a typo error-if comfirm is 2:
So I'm coding a small project and I'm struggling with a certain aspect so far.
Here is the code:
import re
def clientDetails():
print("Welcome to the InHouse Solutions Room Painting Price Calculator")
print("STEP 1 - CLIENT DETAILS")
print("Please enter your full name")
userName = input(">>>")
print("Please enter your post code")
postCode = input(">>>")
print("Is your house a number, or a name?")
nameOrNumber = input(">>>")
if nameOrNumber == "number" or nameOrNumber == "Number":
print("Please enter your house number")
houseNumber = input(">>>")
elif nameOrNumber == "Name" or nameOrNumber == "name":
print("Please enter your house name")
houseName = input(">>>")
else:
print("Invalid")
house = (houseNumber) + (houseName)
address = (postCode) + ", " + (house)
print("Thank you for your information")
print (userName)
print (address)
print (" ")
print ("Is this information correct? Pleast enter Yes or No")
clientDetailsCorrect = input(">>>")
if clientDetailsCorrect == "no" or clientDetailsCorrect == "No":
clientDetails()
clientDetails()
Not sure what's going wrong as I haven't actually referenced the variable anywhere else. Someone help.
It would help if you posted the traceback.
That said, this line is the likely source of the problem:
house = (houseNumber) + (houseName)
The way your code is currently written, only one of houseNumber or houseName will be defined. So Python is likely complaining about the missing one.
Given how your code looks so far, it's probably better to just do:
print("Please enter your house name or number")
house = input(">>>")
And remove the house = (houseNumber) + (houseName) line.
Try this:
def clientDetails():
print("Welcome to the InHouse Solutions Room Painting Price Calculator\n")
print("STEP 1 - CLIENT DETAILS")
print("Please enter your full name")
userName = raw_input(">>>")
print("Please enter your post code")
postCode = raw_input(">>>")
print("Please enter your hose number or name")
house = raw_input(">>>")
address = "{}, {}".format(postCode, house)
print("Thank you for your information.\n")
print (userName)
print (address)
print (" ")
print ("Is this information correct? Pleast enter Yes or No")
clientDetailsCorrect = raw_input(">>>")
if clientDetailsCorrect.lower().startswith('n'):
clientDetails()
Using raw_input is better, it will input everything as a string. Also it will allow users to not type quotations to input text (I assume you will run this from CLI). If you later on need to separate houses that are numbers from names Python has very good string methods you can use to do so many wonderful things, I used a couple of them to simplify your code :)
N