Jul-11-2020, 03:06 AM
Hello Python community,
I'm new around these parts, and I've picked up on Python last week to begin understanding and writing working programs. I have been referencing the book "Automate the Boring Stuff" while I learn to program, but decided to pause at Chap4 to write some programs of my own.(taking off the training wheels).
The program I'm trying to write is a simple database that asks for your First Name, Last Name, email address, and birthday. Once all information is acquired, it is stored inside a database list. Once the program ends, it prints the database list(just to know it works without recalling the database).
Result:
I have succeeded in doing this part, but now I want to carry out this process 3 times. Each time the cycle is carried out, I want a new row/column to be appended inside the database list.
Intended Result:
I have gone online to look for answers to this problem, and came across some possible solutions, but they are obscure to me; thus hard for me to apply to the code that I had already written:
Side note: I feel like I wrote more lines than I needed to to carry the process out. If I did, my apologies. I was really focussed on making the program make logical sense on the end-user side.
I'm new around these parts, and I've picked up on Python last week to begin understanding and writing working programs. I have been referencing the book "Automate the Boring Stuff" while I learn to program, but decided to pause at Chap4 to write some programs of my own.(taking off the training wheels).
The program I'm trying to write is a simple database that asks for your First Name, Last Name, email address, and birthday. Once all information is acquired, it is stored inside a database list. Once the program ends, it prints the database list(just to know it works without recalling the database).
Result:
['John Doe', '[email protected]', 'Jan 1 1996']I have succeeded in doing this part, but now I want to carry out this process 3 times. Each time the cycle is carried out, I want a new row/column to be appended inside the database list.
Intended Result:
[['John Doe', '[email protected]', 'January 1 1996'],['Johnny Appleseed', '[email protected]', 'January 2 1996']]I have gone online to look for answers to this problem, and came across some possible solutions, but they are obscure to me; thus hard for me to apply to the code that I had already written:
import time, sys
database = []
firstName = ''
lastName = ''
email = ''
name = []
birthday = []
months = ['January','February','March','April','May','June','July',
'August','September','October','November','December']
print('Database')
print('\n')
# first name
while True:
print('Please enter your first name. ' , end='')
print('(Cannot include special characters)')
firstName = input()
if firstName.isdigit(): # validates characters
print('Invalid character found')
time.sleep(1.0) # 1 second pause before re-prompting name
else:
name.append(firstName.upper())
break
# last name
while True:
print('Please enter your last name. ' , end='')
print('(Cannot include special characters)')
lastName = input()
if lastName.isdigit(): # validates characters
print('Invalid character found')
time.sleep(1.0) # 1 second pause before re-prompting name
else:
name.append(lastName.upper())
break
# Concatenates first and last name strings into
# One string. Appends the 'name' variable to database
# as database[0]
name = name[0] + ' ' + name[1]
database.append(name)
# email
while True:
# validates email
input_email = input('What is your email address? ')
if '@' not in input_email:
print("Make sure to include the '@' in your email address. " , end='')
print("Please try again")
time.sleep(1.0)
else:
database.append(input_email)
break
# birthday
while True:
# validates birth month
input_month = input('What is your month of birth? ')
if input_month not in months:
print('Invalid month. Please try again.')
time.sleep(1.0)
else:
birthday.append(input_month)
break
while True:
# validates birth day
try:
input_day = int(input('On what date were you born? '))
if input_day >= 32:
print('Invalid date. Please try again.')
time.sleep(1.0)
else:
birthday.append(input_day)
break
except ValueError: # makes sure that string is not passed as an input
print('Invalid date. Please try again.')
time.sleep(1.0)
input_day = int(input('On what date were you born? '))
while True:
# validates birth year
try:
input_year = int(input('What year were you born? '))
if input_day >= 2021:
print('This year has not happened yet. Please try again.')
time.sleep(1.0)
else:
birthday.append(input_year)
break
except ValueError: # makes sure that string is not passed as an input
print('Invalid entry. Please try again.')
time.sleep(1.0)
input_day = int(input('What year were you born? '))
# concatenates 'birthday' list
# & appends them to database as
# database[2]
birthday = birthday[0] + ' ' + str(birthday[1]) + ' ' + str(birthday[2])
database.append(birthday)
print(database)What can I add to my pre-existing code that won't break the program when ran? Side note: I feel like I wrote more lines than I needed to to carry the process out. If I did, my apologies. I was really focussed on making the program make logical sense on the end-user side.
