我基本上是在运行一个代码,通过用户条目将地址簿构建成文本文件。你知道吗
在这样做的同时,我检查输入的信息是否正确,如果不正确,请他们改正。然而,我意识到用户可能(尽管不太可能)无限次地输入不正确的信息,因此我希望实现一个“while”循环来解决这个问题。你知道吗
在下面的代码中,我基本上是在尝试使用它,这样就可以通过检查布尔值“first”而不是第一个ifelse条目进入循环_姓名:isalpha():". 然而,我真的想不出一个方法来进入它当“第一次”_姓名:isalpha():“是真的,我不需要进入循环,因为输入是正确的。当它为false时,我们完全跳过循环而不更正条目。你知道吗
这基本上会引发这样一个问题:当布尔值为false时,是否有方法进入循环。或者,如果我没有考虑另一个创造性的解决方案。你知道吗
谢谢你
初学者
NewContact = "New Contact"
def NewEntry(NewContact):
# Obtain the contact's information:
First_Name = input("Please enter your first name: ")
Last_Name = input("Please enter your last name: ")
Address = input("Please enter your street address: ")
City = input("Please enter your city of residence: ")
State = input("Please enter the 2 letter abbreviation of your state of residence: ")
ZipCode = input("Please enter your zip code: ")
Phone_Number = str(input("Please enter your phone number: "))
# Ensure all information inputted is correct:
if First_Name.isalpha():
First_Name = First_Name.strip()
First_Name = First_Name.lower()
First_Name = First_Name.title()
else:
First_Name = input("Please reenter your first name. Be sure to to include letters exclusively: ")
if Last_Name.isalpha():
Last_Name = Last_Name.strip()
Last_Name = Last_Name.lower()
Last_Name = Last_Name.title()
else:
Last_Name = input("Please reenter your first name. Be sure to to include letters exclusively: ")
# Organize inputted information:
NewContact = Last_Name + ", " + First_Name
FullAddress = Address + " " + City + ", " + State + " " + ZipCode
# Return information to writer to ensure correctness of information
# Write information onto document
TheFile = open("AddressBook", "w")
TheFile.write(str(NewContact) + "\n")
TheFile.write(str(FullAddress) + "\n")
TheFile.write(str(Phone_Number) + "\n")
TheFile.close()
NewEntry(NewContact)
您正在寻找
not
运算符,它反转布尔值:可以将它固定在任何表达式的前面,该表达式是
if
或while
的有效条件。你知道吗使用此结构
相关问题 更多 >
编程相关推荐