[英]How to use a while loop while adding to your dictionary?
My dictionary:我的词典:
company_account = {
"CompanyKey": ["Company", "Address", 321321, "City", 14575159920, "Name"]
}
User input of another key and values assigned:用户输入另一个键和分配的值:
company_account[input("Enter company key: ")] = input("Enter company name: "), input("Enter company address: "), input("Enter company city code: "), input("Enter city: "), ---> ID <---, input("Enter owners name: ")
While loop that restricts the user to only enter a number that's len == 11
: While 循环限制用户只能输入len == 11
的数字:
while True:
ID = input("Enter companys ID: ")
if len(ID) == 11:
print(ID)
break
else:
print("ID has to have 11 digits. Try again.")
My question is: how can I put this while loop in company_account
that updates the dictionary, instead of ID
?我的问题是:如何将这个 while 循环放在更新字典的company_account
中,而不是ID
中? Or maybe some other solution to restricting the user to enter a number that's exactly 11 digits long while updating the dictionary?或者也许是其他一些解决方案来限制用户在更新字典时输入恰好 11 位数字的数字?
I tried putting global ID
, but it returns an error.我尝试放置global ID
,但它返回错误。
Your loop for getting the input is good.您获取输入的循环很好。 Just make it a function and use it instead of the standard input
in the key assignment:只需将其设为函数并使用它代替键分配中的标准input
:
def get_id():
while True:
ID = input("Enter companys ID: ")
if len(ID) == 11:
return ID
else:
print("ID has to have 11 digits. Try again.")
company_account[input("Enter company key: ")] = [input("Enter company name: "), ..., get_id(), ...]
Something like this?是这样的吗? Made your len-check a function and took your question af and put them in the dict.使您的 len-check 成为一个功能,并提出您的问题并将它们放入字典中。 Easier to read if you ask me.如果你问我,更容易阅读。
Also added to check_len function to make sure the input is int.还添加到 check_len 函数以确保输入是 int。 Did same for city code.对城市代码也一样。
def check_len(description):
while True:
ID = input(description)
if len(ID) == 11:
try:
return int(ID)
except ValueError:
print('ID can only contain digits.')
else:
print("ID has to have 11 digits. Try again.")
def check_int(description):
while True:
ID = input(description)
try:
return int(ID)
except ValueError:
print('ID can only contain digits.')
a = check_len('Enter Company key: ')
b = input('Enter Company name: ')
c = input('Enter Company address: ')
d = check_int('Enter Company City Code: ')
e = input('Enter City: ')
f = input('Enter owners name: ')
company_account = {
"CompanyKey": [a, b, c, d, e, f]
}
print(company_account)
Output:输出:
Enter Company key: 12345678910
Enter Company name: Useless Apps
Enter Company address: Useless Street
Enter Company City Code: 1337
Enter City: Useless Town
Enter owners name: Mr Useless
{'CompanyKey': [12345678910, 'Useless Apps', 'Useless Street', 1337, 'Useless Town', 'Mr Useless']}
I'd definitely recommend you to look into Classes and Objects though.不过,我肯定会建议您查看类和对象。
Oof.钱币。 You're chopping down a tree with a hacksaw.你正在用钢锯砍树。 We need to get you some power tools!我们需要给你买些电动工具!
I'll give you something in 'vanilla' Python 3.6+, while mentioning that there are third party libraries out there that can help you.我会给你一些“香草”Python 3.6+ 的东西,同时提到有第三方库可以帮助你。
class CompanyAccount:
def __init__(self):
self.key=self.ask_for("key")
self.name=self.ask_for("name")
self.address=self.ask_for("address")
self.city_code=self.ask_for("City Code")
self.city=self.ask_for("City")
self.owner=self.ask_for("owner's name")
self.id=self.ask_for_id()
@staticmethod
def ask_for_id():
while True:
ID = self.ask_for("ID")
if len(ID) == 11:
break
else:
print("ID has to have 11 digits. Try again.")
# break out of loop here
return ID
@staticmethod
def ask_for(val):
return input(f"Enter Company {val}: ")
def to_dict(self):
return {
self.key: [
self.name,
self.address,
self.city_code,
self.city,
self.id,
self.owner,
]
}
# initialise
company = CompanyAccount()
print(company.id) # etc.
company.to_dict() # in case the format you asked for is really important
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.