I am working on an assignment for my Beginning Python class. The assignment is fairly simple, receive the first name, last name, address line 1, address line 2, city, state, zip and print it out but only print address line 2 if it contains information, otherwise skip it. I worked out some code to make it work, but the problem is that when it prints the information if there is no data in address line 2 the console shows a blank line. then prints the city, state, and zip on the next line. this is an online class at school so i'm basically teaching myself and just winging it as i go. i would appreciate any help y'all can give me.
title = "Address Application"
print(title)
def main():
global fname
global lname
global address1
global address2
global city
global state
global zip
fname = input("Enter your first name: ")
lname = input("Enter your last name: ")
address1 = input("Enter your street address: ")
address2 = input("If you had an additional address line, enter: ")
city = input("Enter your city: ")
state = input("Enter your 2 letter state abbreviation: ")
while len(state) != 2:
state = input("Please enter the 2 letter state abbreviation: ")
if len(state) ==2:
break
zip = input("Enter your zip code: ")
beautify()
# This function cleans up the user's input and outputs with proper capitalization
def beautify():
fname_cap = fname.capitalize()
lname_cap = lname.capitalize()
address1_cap = address1.title()
address2_cap = address2.title()
city_cap = city.capitalize()
state_cap = state.upper()
print("=" * 80)
print(fname_cap, lname_cap)
print(address1_cap)
if address2_cap != None:
print(address2_cap)
print(city_cap,",", state_cap, zip)
print("=" * 80)
end()
def end():
end_result = input("Would you like to enter another address? Y or N ")
if end_result.lower() == "n":
exit()
else:
print()
print("*" * 80)
main()
main()
When the user does not input the address2
, the value of address2
is an empty string rather than None . And so, you see the blank.
Hence, try
if address2_cap != '':
print(address2_cap)
instead
if address2_cap != None:
print(address2_cap)
title = "Address Application"
print(title)
def main():
global fname
global lname
gLobal address1
global address2
global city
global state
global zip
fname = input("Enter your first name: ")
lname = input("Enter your last name: ")
address1 = input("Enter your street address: ")
address2 = input("If you had an additional address line, enter: ")
city = input("Enter your city: ")
state = input("Enter your 2 letter state abbreviation: ")
while len(state) != 2:
state = input("Please enter the 2 letter state abbreviation: ")
if len(state) ==2:
break
zip = input("Enter your zip code: ")
beautify()
This function cleans up the user's input and outputs with proper capitalization
def beautify():
fname_cap = fname.capitalize()
lname_cap = lname.capitalize()
address1_cap = address1.title()
address2_cap = address2.title()
city_cap = city.capitalize()
state_cap = state.upper()
print("=" * 80)
print(fname_cap, lname_cap)
print(address1_cap)
if (len(address2_cap) != 0):
print(address2_cap)
print(city_cap,",", state_cap, zip)
print("=" * 80)
end()
def end():
end_result = input("Would you like to enter another address? Y or N ")
if end_result.lower() == "n":
exit()
else:
print()
print("*" * 80)
main()
main()
Use a truthy check for address2
, eg if address2
rather than if address2 != None
.
I'd also suggest avoiding the use of global
and having fewer variables in general.
def main():
print("Address Application")
fname = input("Enter your first name: ")
lname = input("Enter your last name: ")
address1 = input("Enter your street address: ")
address2 = input("If you had an additional address line, enter: ")
city = input("Enter your city: ")
state = ""
while len(state) != 2:
state = input("Please enter the 2 letter state abbreviation: ")
zip_code = input("Enter your zip code: ")
beautify(
fname,
lname,
address1,
address2,
city,
state,
zip_code
)
if should_repeat():
main()
def beautify(
fname,
lname,
address1,
address2,
city,
state,
zip_code
):
"""
This function cleans up the user's input and pretty-prints it
"""
print("=" * 80)
print(fname.capitalize(), lname.capitalize())
print(address1.title())
if address2:
print(address2.title())
print(f"{city.capitalize()}, {state.upper()} {zip_code}")
print("=" * 80)
def should_repeat():
if input(
"Would you like to enter another address? Y or N "
).lower() == "n":
return False
print()
print("*" * 80)
return True
if __name__ == '__main__':
main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.