我正在为我的 Python 入门课程做作业。分配相当简单,接收名字,姓氏,地址行1,地址行2,城市,州,邮政编码并打印出来,但如果包含信息,则只打印地址行2,否则跳过它。我编写了一些代码来使它工作,但问题是当它打印信息时,如果地址行 2 中没有数据,控制台会显示一个空行。然后在下一行打印城市、州和邮编。这是学校的在线课程,所以我基本上是在自学,只是边走边学。我很感激你们能给我的任何帮助。
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()
当用户没有输入address2
,的值address2
是一个空字符串,而不是无。所以,你看到了空白。
因此,尝试
if address2_cap != '':
print(address2_cap)
反而
if address2_cap != None:
print(address2_cap)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句