This code executes well, but I don't think this the best way to do it.
def bank_account(Name, number, username, passwrod):
Output = Name, number, username, passwrod
return Output
new_name = input ("Please Enter Your Name: ")
new_number = input ("What's your number please: ")
new_username = input ("Enter your UserName: ")
new_passwrod = input ("(Integers Only!) Enter your Passwrod Sir: ")
print("Your Data has been saved!", bank_account(new_name, new_number, new_username, new_passwrod))
Is there any other way to do it?
As the comments suggest, what you're trying to do is unclear!
Maybe something the bank_account function can do is validate the input?
For example you specify that the password must be numeric. For arguments sake let's say the same is true for the number. Also just assume that the name must only consist only of alphabetic letters.
def bank_account(name, number, username, password):
if not name.isalpha():
print('Your name must only contain letters!')
return 'Invalid'
try:
number = int(number)
password = int(password)
except ValueError:
print('Your number and password must be numeric!')
return 'Invalid'
return name, number, username, password
new_name = input("(alphas) Please Enter Your Name: ")
new_number = input("(ints) What's your number please: ")
new_username = input("Enter your UserName: ")
new_password = input("(ints) Enter your Password: ")
print("Your details:", bank_account(new_name,
new_number,
new_username,
new_password))
Hope this gives you an idea of something you can do with your bank_account function!
Related
I am new to python, just start learning :P
I am trying to create a password protected program, but I am stuck in assigning multiple string values to single variable.
can anyone help me solving this problem,
If you guys have better idea for this "login type" program please guide me !
I want to assign all different possible synonyms assign to one variable so it become easy for user to enter!!!
(my English :P)
#Code 1
User_Name = "Allex_Benzz", "allex benzz", "allex_benzz", "Allex Benzz"
User_Input = input("Please Input Your User Name...!\n")
if User_Input == User_Name:
User_Password = "0011"
User_Input_Password = input("Please Enter Your Password...!\n")
if User_Input_Password == User_Password:
print("Welcome Allex Benzz")
else:
print("Your Password Is Incorrect..!")
else:
print("No Users Found")
Result Code 1
Please Input Your User Name...!
allex benzz #(User Input)
No Users Found
#Python login
#Code 2
User_Name = "Allex_Benzz"
User_Name = "allex benzz"
User_Name = "allex_benzz"
User_Name = "Allex Benzz"
User_Input = input("Please Input Your User Name...!\n")
if User_Input == User_Name:
User_Password = "0011"
User_Input_Password = input("Please Enter Your Password...!\n")
if User_Input_Password == User_Password:
print("Welcome Allex Benzz")
else:
print("Your Password Is Incorrect..!")
else:
print("No Users Found")
#Result Code 2
#In this case its is only working if I use Allex Benzz because User_Name is setted to Allex Benzz
Please Input Your User Name...!
allex benzz #User_Input
No Users Found
#working Result for Code 2
Please Input Your User Name...!
Allex Benzz #User_Input
Please Enter Your Password...!
0011 #User_Password
Welcome Allex Benzz
In this code:
User_Name = "Allex_Benzz", "allex benzz", "allex_benzz", "Allex Benzz"
User_Input = input("Please Input Your User Name...!\n")
if User_Input == User_Name:
# ...
User_Name is a tuple of multiple strings, and User_Input is a single string. A single string will never be equal to (==) a tuple, but it can be in a tuple. Change the if check to:
if User_Input in User_Name:
and it should work the way you intend.
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 have been working on this for too long. It should be simple and I've ran through many different combinations, however, I keep getting the code wrong and have no idea why. It works fine when I have manual input, but when I submit there is an error.
question prompt:
Write a program that keeps a dictionary of names and their corresponding phone numbers.
Repeatedly ask the user for a name. Then, do one of the following three things, depending on what they enter:
If they enter nothing, exit the program.
If they enter a name that exists as a key in your dictionary, simply print the corresponding phone number.
If they enter a name that is NOT in your dictionary as a key, ask the user for a phone number, and then put the name and phone number in your dictionary.
Print out the final dictionary.
my code:
phoneBook = {}
name = input("Please enter a name(or press enter to end input): ")
while name != '':
if not name in phoneBook:
number = input("Please enter number: ")
print "Phone number: " + number
phoneBook[name] = number
name = input("Please enter a name(or press enter to end input): ")
if name in phoneBook:
print phoneBook[name]
if name == '':
break
print phoneBook
Expected result:
Phone number: 1234
Phone number: 5678
{'Tracy': '5678', 'Karel': '1234', 'Steve': '9999'}
My result:
Phone number: 1234
Phone number: 5678
Phone number: 9999
1234
Phone number: 9999
5678
Phone number: 9999
{'Tracy': '9999', 'Karel': '9999', 'Steve': '9999'}
you must also access the dictionary keys for checking the existence of a name:
if not name in phoneBook.keys():
phoneBook = {}
name = input("Please enter a name(or press enter to end input): ")
while name != '':
if not name in phoneBook.keys():
number = input("Please enter number: ")
phoneBook[name] = number
print "Phone number: " + number
name = input("Please enter a name(or press enter to end input): ")
else:
print phoneBook[name]
print phoneBook
Try the above code.
When a name is not in the phoneBook, then assign the name a number, so phoneBook[name] = number should be in the if not name in phoneBook.keys(): block. And then enter another name in the same if block.
The issue I keep having is that after I register a username/password, then try to login if I get any letters or numbers of the login/password correct it accepts it, for example if my username is Fof and my Password is tog and I enter the username as just f or o it will accept it.
Here's the code written in Python idle 3.7:
if Game == "1":
username = input("Please enter your username: ")
if username in open("Names.txt").read(): #fix
print ("Welcome " + username)
password = input("Please enter your password: ")
if password in open("Passwords.txt").read():
print ("success!")
else:
print("Username incorrect!")
An explanation of what you need:
You need to look for the exact match of the word in the file, not just in because that would always return True and hence it would bypass:
An example:
NamesList:
Fof
Abc
Def
and then:
import re
text = input("enter Name to be searched:")
NamesFile = open("NamesList.txt", "r")
for line in NamesFile:
if re.search(r"\b" + text + r"\b", line):
print(line)
else:
print("Name not found")
break
OUTPUT:
enter Name to be searched:Fof
Fof
In another case:
enter Name to be searched:f
Name not found
If you store logins and passwords the way you do, then one user can use password of another user and vice versa. It's better to store login-password pair together:
File credentials.json:
{"Fof": "tog"}
Code:
import json
with open('credentials.json') as f:
credentials = json.load(f)
username = input('Please enter your username: ')
if credentials.get(username):
print('Welcome {}'.format(username))
password = input('Please enter your password: ')
if credentials[username] == password:
print('success!')
else:
print('Username incorrect!')
Let's try to hack:
Please enter your username: f
Username incorrect!
Successful login:
Please enter your username: Fof
Welcome Fof
Please enter your password: tog
success!
I am making a password validator/checker program as part of my computing assignment.It must have an uppercase and lowercase letter and be at least 8 characters long.
So far I have done this:
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
while new_password != new_password2:
print("The passwords don't match up.")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
if new_password == new_password2:
length = len(new_password)
while int(length) < 8:
print("Your password must be longer")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
letters = set(new_password)
lower = any(letter.islower() for letter in letters)
while new_password == new_password2:
if not lower:
print("Your password must contain a lowercase letter")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
upper = any(letter.isupper() for letter in letters)
while new_password == new_password2 :
if not upper:
print("Your password must contain an uppercase letter")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
The code runs, but for some reason, the while loops do not work as even if the condition is right,( eg. the password contains an uppercase letter), the option for the user to enter the password again is being displayed. Can someone take a look and tell me the problem here? Thanks
You want to only have one while loop in which every requirement is checked. See the following code:
valid_password = False
while not valid_password:
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
if new_password != new_password2:
print("The passwords don't match up.")
continue
elif len(new_password) < 8:
print("Your password must be longer")
continue
elif new_password.upper() == new_password or new_password.lower() == new_password:
print("Your password must contain at least one lowercase and uppercase letter")
continue
else:
print("Password Accepted!")
valid_password = True
Hope this helps!
a= int (input("Enter Passcode: "))
if a == 1974:
print (" Welcome!! ")
else:
print ("Wrong Passcode")
print ("Run again")