2016-12-04 57 views
0
thing = 0 
while True: 
    code = raw_input("") 
    thing += len(code) 
    if code == "doubler": 
     thing += len(code) * 2 

我想要加倍thing的增長率。但如果我寫thing = len(code)*2它將值重置爲輸入的兩倍。如果我寫thing += len(code)*2它只是增加了兩倍的輸入,並沒有影響其他輸入。改變增長率

回答

0

如果我正確理解你的問題,你想實現的是在一個類型'doubler'之後,乘法應該對所有將來的輸入進行。如果是這樣,也許你正在尋找的東西是一樣的東西

thing = 0 
doubling = False 
while True: 
    code = raw_input("") 
    if code == "doubler": 
    doubling = True 
    thing += len(code)*2 if doubling else len(code) 
0

好?

thing = 0 
add = 1 
limit = 50 
while limit > 0: 
    print thing 
    thing += add 
    add = add * 2 
    limit -= 1 
0

我假設你想要的是每當code等於code=="doubler",併爲未來的投入以及實現翻番len(code)效果。那麼你需要跟蹤你遇到了多少次"doubler"。以下可能適用於您:

thing = 0 
scale = 1 
while True: 
    code = raw_input("") 
    thing += scale*len(code) 
    if code == "doubler": 
     scale *= 2 
+0

這可能是工作,但我想添加更多像「doubler」。如果你找到更好的方法,請寫信給我。感謝您的建議。它工作順便。 –

+0

你更喜歡「doubler」是什麼意思?你想添加另一個關鍵字爲相同的效果? –

+0

是的有點。我想添加如增加=增加* 5% –