我正在尝试使while循环正常运行,而且我还在努力弄清楚如何从用户那里获得多个电话号码输入,然后结束循环
我尝试将ct等同于num和其他变量
我希望代码:
ct=0
while ct<=6:
phonenumbertype= input(' choose one of the following number types cell,work , other : ')
areacode= int(input(' Please enter your areacode :'))
phonenumberexchange= int(input(' Please enter first three numbers after area code :'))
linenumber= int(input(' Please enter the last 4 digits / numbers: '))
Answer = input('Would you like to enter another number? you only have two slots left: Y or N')
答案 0 :(得分:0)
Answer
的文本,在您的问题中,似乎目标是收集一个数字并为用户提供再输入两个数字的机会。
cell
,work
和other
都有一个数字,这意味着while loop
最多应连续3次,而不是6次。while loop
应该停止,或者该人输入了n
或no
的任何大写或小写字母dict
将用于存储每个条目dict
可以采用的多种形式之一ct = 0
phone_numbers = {'type': [],
'area': [],
'exchange': [],
'line': []}
numbers_allowed = 3
while True:
phone_numbers['type'].append(input('Choose one of the following number types cell, work , other: '))
phone_numbers['area'].append(int(input('Please enter your area code: ')))
phone_numbers['exchange'].append(int(input('Please enter first three numbers after area code: ')))
phone_numbers['line'].append(int(input('Please enter the last 4 digits / numbers: ')))
ct+=1
if ct == numbers_allowed:
print('You have entered all the available numbers. Thanks')
break
Answer = input(f'You may enter {numbers_allowed - ct} more numbers. Y, to enter another number, otherwise N: ')
if Answer.lower() in ['n', 'no']:
break
print(phone_numbers)
Choose one of the following number types cell, work , other: cell
Please enter your area code: 111
Please enter first three numbers after area code: 111
Please enter the last 4 digits / numbers: 1111
You may enter 2 more numbers. Y, to enter another number, otherwise N: y
Choose one of the following number types cell, work , other: work
Please enter your area code: 222
Please enter first three numbers after area code: 222
Please enter the last 4 digits / numbers: 2222
You may enter 1 more numbers. Y, to enter another number, otherwise N: y
Choose one of the following number types cell, work , other: other
Please enter your area code: 333
Please enter first three numbers after area code: 333
Please enter the last 4 digits / numbers: 3333
You have entered all the available numbers. Thanks
{'area': [111, 222, 333],
'exchange': [111, 222, 333],
'line': [1111, 2222, 3333],
'type': ['cell', 'work', 'other']}
Choose one of the following number types cell, work , other: cell
Please enter your area code: 111
Please enter first three numbers after area code: 111
Please enter the last 4 digits / numbers: 1111
You may enter 2 more numbers. Y, to enter another number, otherwise N: n
{'area': [111], 'exchange': [111], 'line': [1111], 'type': ['cell']}
答案 1 :(得分:-1)
使用break
语句。您还没有增加ct
。
我从6开始,因为您要打印插槽数
ct=6
while ct<0:
phonenumbertype= input(' choose one of the following number types cell,work , other : ')
areacode= int(input(' Please enter your areacode :'))
phonenumberexchange= int(input(' Please enter first three numbers after area code :'))
linenumber= int(input(' Please enter the last 4 digits / numbers: '))
choice = input('Would you like to enter another number? you only have ' , ct, ' slots left: Y or N')
if choice.upper() == 'N':
break
ct -= 1