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
Related
print ("Enter your details to create your account ")
Username = input("Enter your username " )
age = input("Enter your Age ")
print("Your username is " + Username)
print("Your age is "+ age)
This is my code, but I'm not sure how to do the "is this information correct" thing.
You were almost there. Something like this should be added to your code and you'll be done.
correct = input("Is this information correct? (y/n)")
And this correct variable will hold a value of "y" or "n" which u can check later in your code if you want.
Just add another prompt at the end like:
data = input("Is this information correct?")
if data.lower() in ['y','ye','yes','yep','yeah']:
# YES
elif data.lower() in ['n','no','nope']:
# No
else:
# Invalid input
One line solution is
verified = True if input("Is this information correct? (y/n)").lower() == 'y' else False
If y or Y entered, verified become True, otherwise False.
EDIT
If you need a loop and get values while not verified by user as you mention in comment, you can use
verified = False
while not verified:
print("Enter your details to create your account ")
username = input("Enter your username: ")
age = input("Enter your Age ")
print("Your username is " + username)
print("Your age is " + age)
verified = True if input("Is this information correct? (y/n)").lower() == 'y' else False
I am writing a program in python for a banking application using arrays and functions. Here's my code:
NamesArray=[]
AccountNumbersArray=[]
BalanceArray=[]
def PopulateAccounts():
for position in range(5):
name = input("Please enter a name: ")
account = input("Please enter an account number: ")
balance = input("Please enter a balance: ")
NamesArray.append(name)
AccountNumbersArray.append(account)
BalanceArray.append(balance)
def SearchAccounts():
accounttosearch = input("Please enter the account number to search: ")
for position in range(5):
if (accounttosearch==AccountNumbersArray[position]):
print("Name is: " +NamesArray[position])
print(NamesArray[position],"account has the balance of: ",(BalanceArray[position]))
break
if position>4:
print("The account number not found!")
while True:
print("**** MENU OPTIONS ****")
print("Type P to populate accounts")
print("Type S to search for account")
print("Type E to exit")
choice = input("Please enter your choice: ")
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
break
else:
print("Invalid choice. Please try again!")
Everything is fine now, but when I run the program and search for an account. For example, when I search an account the output shows ('name', 'account has the balance of: ', 312) instead of name account has the balance of: 312 How do I fix this?
In the line
print(NamesArray[position],"account has the balance of: ",(BalanceArray[position]))
you should use string concatenation to add the different strings. One way to do this would be like this:
print(NamesArray[position] + " account has the balance of: " + str(BalanceArray[position]))
u are using old python version,
replace your line with this :
print(NamesArray[position] + "account has the balance of: " + (BalanceArray[position]))
use ' + ' instead of comma
I was messing around and wanted to see if I could get this simple program to work. I want to check the user input against my list and output whether or not it is in the list. How I have it setup up currently it will return for the 0 index but not the other two. What am I doing wrong?
print("Check to See if You are Registered:")
name = input("Please Enter Your Name:")
check_name = ["lisa", "jim", "betty"]
if name == check_name[0]:
print("Welcome lisa")
if name == check_name[1]:
print("Welcome jim")
elif name == check_name[2]:
print("Welcome betty")
pass
else:
print("Sorry, Looks like you are not in the system")
pass
Your last two if statements are only evaluating nested inside of the first one. You need to unindent them so that they'll evaluate if the first does not.
Another solution:
print("Check to See if You are Registered:")
name = input("Please Enter Your Name:")
check_name = ["lisa", "jim", "betty"]
if name not in check_name:
print("Sorry, Looks like you are not in the system")
You have an indentation problem.
print("Check to See if You are Registered:")
name = input("Please Enter Your Name:")
check_name = ["lisa", "jim", "betty"]
if name == check_name[0]:
print("Welcome lisa")
elif name == check_name[1]:
print("Welcome Jim")
elif name == check_name[2]:
print("Welcome Betty")
else:
print("Sorry, Looks like you are not in the system")
You already got your answer above. Here is an alternate solution negating the need of multiple if statements making your code concise:
print("Check to See if You are Registered:")
name = input("Please Enter Your Name:")
check_name = ["lisa", "jim", "betty"]
if name in check_name:
print("Welcome %s" %name)
else:
print("Sorry, Looks like you are not in the system")
The command if name in check_name: checks if the input name exists in the name list and welcomes the corresponding person. If the input name doesn't exist, it throws the Sorry... statement
This is my atm, it has a pin code protection thing and can basically run like a normal atm, however i need to make it so it remembers you and your balance everytime you log out and in etc. ive tried doing this with a blank text file(nothing is in the text file) and linked it into my code (as you can see) but it doesnt work, im not sure what i have to add etc. any help?
balance = float(0)
userInput = None
path = 'N:\ATM.txt'
username = 'some_username'
with open(path, 'r') as file:
for user in file.readlines():
if user == username:
print("welcome back")
print("Hello, Welcome to the ATM")
print("")
print("Please begin with creating an account")
name = raw_input("Enter your name: ")
saved_code = str(raw_input("Please enter a 4 digit pin to use as your passcode: "))
try:
int(saved_code)
if len(saved_code)!=4:
raise
except Exception, e:
print("Error: Pin is not a valid 4 digit code")
exit()
totalTrails = 3;
currentTrail = 0;
status = 1;
while currentTrail < totalTrails:
user_code =str(raw_input('Please enter the 4 digit pin on your card:'))
if user_code==saved_code:
status=0
break;
else:
currentTrail+=1
if status==0:
print("correct pin!")
else:
print("You tried to enter a wrong code more than three times.")
exit();
print "Hello , welcome to the ATM"
while userInput != "4":
userInput = raw_input("\n what would you like to do?\n\n (1)Check balance\n (2)Insert funds\n" +
" (3)Withdraw funds\n (4)Exit the ATM\n" )
if userInput == "1":
print "your balance is", "£" , balance
elif userInput == "2":
funds = float(raw_input("Enter how much money you want to add"))
balance = balance + funds
elif userInput == "3":
withdraw = float(raw_input("Enter how much money you want to withdraw..."))
balance = balance - withdraw
elif userInput == "4":
print "Thanks for using the ATM!"
You are opening the file in 'r' mode, which means read only, you should use 'r+' if you want to read and write in it.
You are not writing anything to the file - that's done by write() method - in your case
file.write("string I want to write to the file");
After you are done writing to the file, close it - file.close()
Im trying to create a pin protection thing for my ATM and you can enter a code etc. It then asks you for it however I want it so if you enter the wrong passcode it says incorrect try again and you cant continue into the main program. Maybe there could be a certain amount of tries? and also don't I have to write to a file if I want to save stuff? but nevermind that unless you know how I could use it in my code....
balance = float(0)
userInput = None
print("Hello, Welcome to the ATM")
print("")
print("Please begin with creating an account")
name = raw_input("Enter your name: ")
code = raw_input("Please enter a 4 digit pin to use as your passcode: ")
code = int(input('Please enter the 4 digit pin on your card:'))
if code == (code): print("correct pin!")
print "Hello , welcome to the ATM"
while userInput != "4":
userInput = raw_input("\n what would you like to do?\n\n (1)Check balance\n (2)Insert funds\n" +
" (3)Withdraw funds\n (4)Exit the ATM\n" )
if userInput == "1":
print "your balance is", "£" , balance
elif userInput == "2":
funds = float(raw_input("Enter how much money you want to add"))
balance = balance + funds
elif userInput == "3":
withdraw = float(raw_input("Enter how much money you want to withdraw..."))
balance = balance - withdraw
elif userInput == "4":
print "Thanks for using the ATM!"
balance = float(0)
userInput = None
print("Hello, Welcome to the ATM")
print("")
print("Please begin with creating an account")
name = raw_input("Enter your name: ")
# take the code as a string
saved_code = str(raw_input("Please enter a 4 digit pin to use as your passcode: "))
#validate that the code is a number and is 4 digit
try:
int(saved_code)
if len(saved_code)!=4:
raise
except Exception, e:
print("Error: Pin is not a valid 4 digit code")
exit()
# set trails and trail counter
totalTrails = 3;
currentTrail = 0;
status = 1;
# take input from user and compare codes
while currentTrail < totalTrails:
user_code =str(raw_input('Please enter the 4 digit pin on your card:'))
if user_code==saved_code:
status=0
break;
else:
currentTrail+=1
if status==0:
print("correct pin!")
else:
print("You tried to enter a wrong code more than three times.")
exit();
print "Hello , welcome to the ATM"
while userInput != "4":
userInput = raw_input("\n what would you like to do?\n\n (1)Check balance\n (2)Insert funds\n" +
" (3)Withdraw funds\n (4)Exit the ATM\n" )
if userInput == "1":
print "your balance is", "$" , balance
elif userInput == "2":
funds = float(raw_input("Enter how much money you want to add"))
balance = balance + funds
elif userInput == "3":
withdraw = float(raw_input("Enter how much money you want to withdraw..."))
balance = balance - withdraw
elif userInput == "4":
print "Thanks for using the ATM!"