While != "specific string" loop mishap - python

I'm trying to make a task a little more efficient. I'm going to continue to research/edit while moving forward.
What the code currently does is ask for your first/last name it then outputs a username & password. Up to this point it works fine if there is a more efficient way to rewrite this also appreciated.
The only thing I still need to fix is looping entries(First_name and Last_name) so I can have multiple users added with a \n\n for easy reading/separation of each user. preferably with a exit command such such as "done"
I'm also going to be looking into how to create a new file that this information is stored in but this is the section I'm going to try to figure out.
First_name = input("Enter your First name: ")
Last_name= input("Enter your last name: ")
Username = str(First_name) + str(Last_name)
Password = Username + str('1!')
print("Username: ", Username.upper())
print("Password: ", Password)
In terms of trying to solve the issue.
I did try to create something in the ball park of..
First_name = input("Enter your First name: ")
Last_name= input("Enter your last name: ")
while First_name != "done":
Last_name= input("Enter your last name: ")
First_name = input("Enter your First name: ")
Last_name= input("Enter your last name: ")
Username = str(First_name) + str(Last_name)
Password = Username + str('1!')
print("Username: ", Username.upper())
print("Password: ", Password)
this just repeated the first two lines without printing the Username/password. It also appeared to acknowledge when I typed done but slightly off from desired. Below I included how the code ran
Enter your First name: first
Enter your last name: last
Enter your last name: last
Enter your First name: done
Enter your last name: last
Username: DONELAST
Password: donelast1!
I get why its first, last, last, first last. I dont get how to make the while loop using "while Fist_name !='done': " without putting lines 1 and 2 before the loop. I tried removing the inputs at line 5 and 6 but then it just looped the username and password.

You can use a while loop and break it when the first name is done.
while True:
first_name = input("Enter your first name (done to exit): ")
if first_name.lower() == "done":
print("Done!")
break
last_name = input("Enter your last name: ")
Username = str(first_name) + str(last_name)
Password = Username + str('1!')
print("Username: ", Username.upper())
print("Password: ", Password)
print("\n")
Output:
Enter your first name (done to exit): John
Enter your last name: Doe
Username: JOHNDOE
Password: JohnDoe1!
Enter your first name (done to exit): Alice
Enter your last name: Court
Username: ALICECOURT
Password: AliceCourt1!
Enter your first name (done to exit): done
Done!

Just set your variables to an empty string before the loop starts. Since an empty string is not equal to done your code will enter the loop and prompt the user:
First_Name = ''
Last_Name = ''
while First_name != "done":
Last_name= input("Enter your last name: ")
First_name = input("Enter your First name: ")
Username = str(First_name) + str(Last_name)
Password = Username + str('1!')
print("Username: ", Username.upper())
print("Password: ", Password)

Related

Is there a way to make an error message for an incorrect value in a field?

I am writing a python code to generate a contract when particular fields are filled
the date field accepts any data instead of dd/mm/yyyy. I want an error message to be generated when an incorrect value is entered and until the right format is entered, the user cannot jump to the next field.
print("Hello, Welcome to Rent contract generator!")
def get():
first = input("Enter your name: ")
last = input("Enter your last name: ")
bday = input("Enter your birthday (dd/mm/yyyy): ")
address = input("Enter your address: ")
userid = input("Enter your ID number: ")
inputdata = (first, last, bday, address, userid)
print("Hello " + first + "! " + "Your rental contract is ready now :)")
return inputdata````

How can I make the program go to the input?

Write a program that asks the user to put the password, first name, last name, and id. If any of that is invalid, program to continuously ask to provide the valid input. Here are the rules:
Password has to be at least 7 characters, must contain at least one uppercase letter, at least one lowercase letter, and at least 1 digit
First name and last name must contain only letters
ID must contain only digits
After you have input everything validly print them on the screen. As the password is sensitive info print only the first three characters and for the rest of the length, print '*'
password = 'Pass1234'
first_name = 'John'
last_name = 'Smith'
ID = '1234'
p = input('Input your password: ')
if (len(p)<6):
print('Invalid password. Must be at least 6 characters')
p = input('Input your password: ')
elif not p.islower():
print('Invalid password. Must have at least 1 lowercase character')
elif not p.isupper():
print('Invalid password. Must have at least 1 uppercase character')
if p == password:
print('Well done! This is a valid password')
password_list = list(password)
password_list[3] = '*'
password_list[4] = '*'
password_list[5] = '*'
password_list[6] = '*'
password_list[7] = '*'
edited_password = ''.join(password_list)
fn = input('Please enter your first name: ')
ln = input('Please enter your last name: ')
for f in fn:
if f.isdigit():
print('Invalid Name! Name should only contain letters')
fn = input('Please enter your first name: ')
for l in ln:
if l.isdigit():
print('Invalid Name! Name should only contain letters')
ln = input('Please enter your last name: ')
if fn == first_name or 'john' and ln == last_name or 'smith':
print('Well done! This is a valid name')
I = input('Please enter your ID: ')
if I.isupper():
print('Invalid ID! ID should only contain numbers')
I = input('Please enter your ID: ')
elif I.islower():
print('Invalid ID! ID should only contain numbers')
I = input('Please enter your ID: ')
elif I == ID:
('Well done! This is a valid ID')
print('Name:', first_name, last_name)
print('ID:', ID)
print('Password:', edited_password)
Output:
Input your password: Pass1234
Invalid password. Must have at least 1 lowercase character
Well done! This is a valid password
Please enter your first name: John
Please enter your last name: Smith
Well done! This is a valid name
Please enter your ID: 1234
Name: John Smith
ID: 1234
Password: Pas*****
How can I fix my program where it doesn't print the lowercase error message?
You can do something like this. The problem you had is you were checking to see if your entire password was lowercase instead of if at least 1 character was.
Of course you'll probably want to change your input variable from "p" to "password" instead of hard coding it at the top of the page.
p = input("Input your password: ")
upper = 0
lower = 0
number = 0
while True:
for x in p:
if x.isupper()==True:
upper+=1
elif x.islower()==True:
lower+=1
elif x.isalpha()==False:
number+=1
if len(p) < 7 or upper <= 0 or lower <= 0 or number <= 0:
print ('password not complex enough')
p = input('please enter password again ')
upper = 0
lower = 0
number = 0
else:
break

Loop statement with two variables

I need to do a program where it will ask first name and then last name. the code should detect if the input is in lowercase letters, if not it will repeat to ask first name and last name.
here is what i did but it does not detect if the input is in lowercase
firstName = input(“Enter your First Name: “)
lastName = input(“Enter your Last Name: “)
fn = (firstName.islower())
ln = (lastName.islower())
while firstName != fn and lastName != lastName != ln:
print (“Your First and Last name should be typed in lowercase letter”)
firstName = input(“Enter your First Name: “)
lastName = input(“Enter your Last Name: “)
print (“yey!”)
Aside from some the silly logical error in the loop condition, your code is good. Here's a corrected version:
firstName = input("Enter your first name: ")
lastName = input("Enter your last name: ")
while not firstName.islower() or not lastName.islower():
print("Your first and last name should be typed in lowercase letters")
firstName = input("Enter your first name: ")
lastName = input("Enter your last name: ")
print ("yey!")
In this code we check if both first and last name are in lowercase if not the user need to try again.
I hope is what you intend to.
first_name = ""
last_name = ""
while True:
# Receive the first name and the last name from the user
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
# check if the first name and the last name are in lower case if yes it exit otherwise print try again
if first_name.islower() and last_name.islower():
break
print("Your first name and last name need to be in lowercase please try again")
print("yay!")
Actually in your first try you was really close to it.
I have made those changes:
No need to do input out of the loop. is just duplicate the code, and make the input in one place make it easier :) .
Need to check if the string is a lowercase inside the loop. and check it for every input you receive until you find a good one.

How to add a space in between the output when reversing the string on python?

This is what I have to do:
Write a Python program which accept the user's first and last name and print them in reverse order with a space between them. THE LETTERS OF THE FIRST AND LAST NAMES SHOULD ALSO BE IN REVERSE ORDER!!!!! For example if a person enters first name is Sam and last name is Murrow then the output should be maS worruM...
-
I can't figure out how to add the space in between firstname & lastname. This is what I have so far:
firstname = input("What is your last name?:")
lastname = input("What's your last?: ")
print (lastname) + (firstname)[::-1]
In your solution you are only reversing the firstname string. What you can do instead is build your full string including both firstname, lastname and a space between them, and then reverse the entire string.
For example:
firstname = input("What is your last name?:")
lastname = input("What is your last?")
print(firstname[::-1] + " " + lastname[::-1])
Try this:
firstname = input("What is your first name?:")
lastname = input("What's your last name?: ")
fn = firstname[::-1]
ln = lastname[::-1]
print (fn,"",ln)
Do this:
first_name = input("Enter your First Name: ")
last_name = input("Enter your Last Name: ")
print(f"{first_name[::-1].strip()} {last_name[::-1].strip()}")
This code will prompt the user to input their First and Last names separately and then store those values in first_name and last_name variables respectively so that they can be used in the print statement (or for other purposes). Also it will remove extra space from input while it will print output
Here's a different approach to solve this problem
print("Enter the name: ")
name = str(input()).split(' ')
namez = name.reverse()
for i in range(len(name)):
print(name[i][::-1], end=" ")
print("{last} {first}".format(last=lastname[::-1], first=firstname[::-1]))
first_name = input("Input your First Name : ")
last_name = input("Input your Last Name : ")
print ("Hello " + last_name + " " + first_name)

How to create a dictionary that I can access throughout the program?

I'm creating a address book program, and need to have a dictionary that I can add too, edit, and delete, as well as pickle. What would be the best way to create it so it is accessible by all the functions? I currently have the dictionary in the addon function but wouldn't it reset if I were to call the dictionary to another function?
My code so far (not including the menuModule)
def addPerson():
personLastName = input("Enter the last name of "
"the person you want to add: ").lower()
personFirstName = input("Please enter the first name of "
"the person you want to add: ")
localPart = input("Please enter the local part of the email address")
while not localPart.isalnum():
localPart = input("Please enter a valid input, a-z and numbers 0-9: ")
domain = input("Please enter the domain of the email addres: ")
while not domain.isalnum():
domain = input("Please enter a valid input, a-z and numbers 0-9: ")
topLevelDomain = input("Please enter the top level domain, examples: com, net, org: ")
while not topLevelDomain.isalnum() or len(topLevelDomain) > 3:
topLevelDomain = input("Please enter only letters, a-z and not more then 3 characters: ")
personEmail = localPart + "#" + domain + "." + topLevelDomain
personStreetAddress = input("Please enter house number and street of the person you want to add: ")
personCityState = input("Please enter the city, state abbreviation and zipcode of the person you want to add: ")
personPhone = input("Please enter the phone number of the person you want to add: ")
personPhoneStr = personPhone.strip("-")
while not personPhoneStr.isdigit() and not len(personPhoneStr) == 10:
personPhone = input("Error. That is not a valid phone number. Try again: ")
personPhoneStr = personPhone.strip("-")
return personLastName, personFirstName, personEmail, personStreetAddress, personCityState, personPhone
def appendDictionary():
personLastName, personFirstName, personEmail, personStreetAddress, personCityState, personPhone = addPerson()
listX = [personFirstName, personEmail, personStreetAddress, personCityState, personPhone]
addressBook = {personLastName: listX}
print(personFirstName,personLastName, "has been added to the address book!")
print(addressBook)
return addressBook
Try using lists. One list for each of the variables because if you try to store them as a tuple and then add them into a master list you will not be able to or it will be hard to charge them and edit them. Here is an example of storing the data:
nameList.extend(john)
emailList.extend(john#gmail.com.uk)
john_index = len(nameList)
Give each person an index to help you file their information so if our list looked like [jane#live.com, sam#wenston.com, john#gmail.com.uk] johns data is going to be the last in the list because we just entered it in position 3 on the list and the length function returns 3 so you know where johns data is stored and if you were to add more data it would stack up accourdingly.
here is an example of getting it back out of the list and editing it:
print nameList[john_index]
print emailList[john_index]
emailList[john_index] = new_value
I hope you understand :)

Categories