简体   繁体   English

从列表+数字添加输入

[英]Add input from list + numbers

Say you have a code like this... 假设您有这样的代码...

answer = input ()
areacode = ['302', '856', '215', '413']
if answer in areacode:
***Looking for what I would put here***

is it possible to have the users input from a list be added to numbers that would count up from 0000000 to 9999999? 是否可以将列表中输入的用户添加到从0000000到9999999的数字上?

Basically I want to ask the user to enter a area code and when they put the area code in, generate every possible outcome of a phone number up to the last number. 基本上,我想让用户输入区号,然后在他们输入区号时,生成电话号码的所有可能结果,直到最后一个号码。 EX: input is 302 so 302 + 0000000, then 302 + 0000001 all the way up to 3029999999 (I know this will take a long time) 例如:输入是302,所以302 + 0000000,然后是302 + 0000001一直到3029999999(我知道这将花费很长时间)

I am using the newest version of Python. 我正在使用最新版本的Python。

for i in range(10000000):
    print(str(answer)+"{0:0=7d}".format(i))

Python supports arbitrarily long integers. Python支持任意长整数。 Just iterate over all these numbers, and add them to the area code multiplied by 10,000,000: 只需遍历所有这些数字,然后将它们添加到区号乘以10,000,000:

for i in range(10000000):
    print(answercode * 10000000 + i);

EDIT: 编辑:
As noted in the comments, redoing the multiplication in every iteration of the loop is wasteful, and be done only once, as the result does not change between iterations: 如注释中所述,在循环的每个迭代中重做乘法都是浪费的,并且只能执行一次,因为结果在迭代之间不会改变:

prefix = answercode * 10000000
for i in range(10000000):
    print(prefix + i);

Should work 应该管用

for i in range(9999999):
        print("answer"+" "+"+"+(str(i).rjust(7, '0')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM