【问题标题】:Identification Code (Python3)识别码 (Python3)
【发布时间】:2017-06-21 07:49:11
【问题描述】:

识别码类似于 GYYMMDD。 G 是 1 或 2,表示男性或女性。 YYMMDD 是生日。

我刚才是这样写的:

s=[]
while True:
  while True:
        s = input('Enter 7 numbers : ')
        if len(s) == 7:
            break
        else:
            print('Retype your 7 personal numbers!!')
  if '0' < s[1] < '3':
    break
  else:
    print('retry')

但是,我不知道将生日代码输入到数组中。

请赐教具体细节。

【问题讨论】:

  • 您的代码中没有array。那是list
  • 好的。我对列表和数组感到困惑。谢谢!

标签: python arrays input while-loop


【解决方案1】:

下面将YY、MM、DD放到一个数组中:

>>> input_ = "1851803"
>>> birthdarray = [input_[i:i+2] for i in range(1, 6, 2)]
>>> birthdarray = ['85', '18', '03']

很明显,您是 python/编程的新手。但是一般来说,最好开始使用函数来验证您的输入,然后接受或拒绝用户的输入;而不是在 while 循环中完成所有操作。

【讨论】:

  • 感谢您的回答,是的,从这个月开始学习。好的,我分开写。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2020-06-02
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多