2012-10-09 97 views
-1
b=int(1) 

if b == 1: 
    b=2 
    c = "on my thumb" 
elif b== 2: 
    b=3 
    c = "on my shoe" 
elif b== 3: 
    b=4 
    c = "on my knee" 
elif b== 4: 
    b+1 
    c = "on my door" 
elif b== 5: 
    b+1 
    c = "on my hive" 
elif b== 6: 
    b+1 
    c = "on my sticks" 
elif b== 7: 
    b+1 
    c = "up in heaven" 
elif b== 8: 
    b+1 
    c = "on my gate" 
elif b== 9: 
    b+1 
    c = "on my spine" 
else: 
    c = "once again" 

for r in range(10): 
    print("This old man, he played one He played knick-knack " + c +" Knick-knack paddywhack, give your dog a bone This old man came rolling home") 
    b+1 

相對較新的編碼,所以我真的不知道我在做什麼,但我試圖讓C到改變R中的每個新的時間被打印......以便IT完成舊的童謠。我在Python編寫此...如何獲得字符串變量來改變和打印出如果循環

+1

那是什麼語言? –

+0

請閱讀[問]或許[您嘗試過什麼?](http://whathaveyoutried.com) – nfechner

+0

將打印語句更改爲「...」+ r +「...'。假設舊編譯器(沒有使用任何語言的經驗),那麼新編譯器將在第一次打印1,第二次打印2,第三次打印3等。 – Smudge202

回答

4
parts = ["on my thumb", "on my shoe", ...] 
numerators = ["one", "two", "three", ...] 

for num, part in zip(numerators, parts): 
    print "This old man, he played " + num 
    print "He played knick-knack " + part 
    print "Knick-knack paddywhack, give your dog a bone" 
    print "This old man came rolling home" 
+0

我喜歡這段代碼,它很簡單有效......但是我該怎麼做才能得到每當它被寫出時改變+1 ...我可以使用一個簡單的if?或者會比我想象的更困難 – LS2012

+0

我不太明白。爲什麼? – defuz

+0

就像循環繼續,我會需要它說:「這個老人,他打了兩個」,「他打三個」等等,抱歉,我沒有說任何正確的......仍然半睡半醒 – LS2012

0

只是存儲您的字符串數組或列表

lines = ["on my thumb", "on my shoe", "...."....] 

,然後遍歷數組(或列表)用的幫助下在C#中環 - ISH語法:

foreach(line in lines) 
{ 
    print("bla" + line + "blubb"); 
} 
0

也許是這樣的:

string b[10] = {"on my thumb","on my shoe","on my knee","on my door","on my hive","on my sticks","up in heaven","on my gate","on my spine","once again"}; 

for (i=0; i<b.length; i++) { 
    print("This old man, he played one He played knick-knack " + b[i] +" Knick-knack paddywhack, give your dog a bone This old man came rolling home.\n"); 
} 

由於這些值不會改變,您可以創建一個靜態數組來保存這些值,然後在顯示結果的同時循環顯示內容。