2017-07-26 353 views
0
def generate_n_chars(n,s="."): 
    res="" 
    count=0 
    while count < n: 
     count=count+1 
     res=res+s 

    return res 

print generate_n_chars(raw_input("Enter the integer value : "),raw_input("Enter the character : ")) 

我是初學者在Python中,我不知道爲什麼這個循環會無限。請人糾正我的程序while循環python導致無限循環

+0

你能添加輸入你用什麼? –

+0

你比較字符串的數字。 –

回答

1

的原因是因爲輸入進行評估,並設置爲一個字符串。因此,您正在比較不同類型的兩個變量。您需要將您的輸入轉換爲整數。

def generate_n_chars(n,s="."): 
    res="" 
    count=0 
    while count < n: 
    count=count+1 
    res=res+s 

generate_n_chars(int(raw_input("Enter the integer value : ")),raw_input("Enter the character : ")) 
+0

謝謝你的工作。 – blb007