C++ for-loop vs. Python for-loop

前端 未结 2 1481
粉色の甜心
粉色の甜心 2021-01-17 00:00

I\'m currently learning Python as I\'m taking a data mining class. I was making a for-loop to make a noisy data file to do smoothing and I found a peculiarity on Python for-

2条回答
  •  鱼传尺愫
    2021-01-17 00:26

    There are two main problems with the python I see;

    Firstly the for loop does not work in the same way as the cpp for loop: python will reset i to go through the range, what you did with i inside the previous loops will be lost. Secondly python does not have a termination char on its strings like cpp does:

    input = "abc defg"
    eachWord = ''
    
    i = 0
    while(i < len(input)):
        print("At the first part of the loop, i = ", i, ".")
        while(i < len(input) and input[i] != ' '):
            eachWord += input[i]
            i += 1
    
        print(eachWord)
        print("At the last part of the loop, i = ", i, ".")
        print()
        eachWord = ''
        i += 1
    

提交回复
热议问题