简体   繁体   中英

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?

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)

I am using the newest version of Python.

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

Python supports arbitrarily long integers. Just iterate over all these numbers, and add them to the area code multiplied by 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')))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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