While循环电话号码

时间:2019-10-23 03:07:28

标签: python-3.x

我正在尝试使while循环正常运行,而且我还在努力弄清楚如何从用户那里获得多个电话号码输入,然后结束循环

我尝试将ct等同于num和其他变量

我希望代码:

  1. 接一个电话号码
  2. 然后询问用户是否要输入更多
  3. 如果他们不这样做
    • 它应该退出循环。
  4. 如果他们这样做
    • 然后应该继续直到数字达到6

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')

2 个答案:

答案 0 :(得分:0)

  • 鉴于Answer的文本,在您的问题中,似乎目标是收集一个数字并为用户提供再输入两个数字的机会。
    • 每个cellworkother都有一个数字,这意味着while loop最多应连续3次,而不是6次。
  • 输入所有3个数字后,while loop应该停止,或者该人输入了nno的任何大写或小写字母
  • 没有说明,但是假设应该存储条目。
    • 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)

输出1:

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']}

输出2:

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