2016-05-13 102 views
-1
from random import randint 
for i in range(5): 
    FN = (input("What is your name?")) 
    firstletter = FN[:1] 
    SN = [] 
    first3letters = SN[:3] 
    x = str(len(SN)) 
    sentence = str(randint(0,9))+(first3letters.lower())+(firstletter.upper()+(x)) 
    print(sentence) 

我需要創建一個數組,它將被打印出來,並由使用此代碼生成的5個用戶名組成。我會怎麼做?如何將數組添加到此?

+1

'SN = []'和'first3letters = SN [:3]',你確定嗎? – SparkAndShine

+0

我這麼認爲?它有錯誤嗎? @sparkandshine –

+1

如果是這樣,'first2letters'總是等於'[]'。這是你想要的情況嗎? – SparkAndShine

回答

1

在使用lower()upper()之前,將firstletterfirst3letters轉換爲字符串。

from random import randint 
for i in range(2): 
    FN = input("What is your name? ") 
    firstletter = ''.join(FN[:1]) 

    SN = 'bMwGermany' 
    first3letters = ''.join(SN[:3]) 
    x = str(len(SN)) 

    # sentence = str(randint(0,9))+(first3letters.lower())+(firstletter.upper()+(x)) 
    sentence = ''.join([str(randint(0, 9)), first3letters.lower(), firstletter.upper(), x]) 
    print(sentence) 

# Test 
$ python3 stackoverflow.py 
What is your name? hello 
1bmwH8 
What is your name? world 
9bmwW8 

注意,對於Python2,使用raw_input代替input。有關詳細說明,請參閱Python input() error - NameError: name '…' is not defined

+0

它不起作用@sparkandshine:/ –

+1

@PennyHarvard,你的意思是'不起作用'?邏輯錯誤還是...? – SparkAndShine

+0

@PennyHarvard,我添加了一個測試用例。 – SparkAndShine