New to Python. Please help!!! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: New to Python. Please help!!! (/thread-19328.html) |
New to Python. Please help!!! - hoangthai10788 - Jun-23-2019 Hello there, I am new to Python. I have been taking a free online course and encountered a problem while self-studying it. With some modifications, I would be able to run the program but still did not know why it happened that way. I really need your feedbacks to help me get a better understanding about Python. Please as detailed as possible. I will post the original problems as well as my code before and after modifications. The problem occurred at while loop in word_mixer function. "while length > 5" got me an error given that I declared length = len(...) as you saw in the before version but while len(...) > 5 worked well. I really appreciate all of your constructive feedbacks. Problems: Program: poem mixer This program takes string input and then prints out a mixed order version of the string Program Parts program flow gathers the word list, modifies the case and order, and prints get string input, input like a poem, verse or saying split the string into a list of individual words determine the length of the list Loop the length of the list by index number and for each list index: if a word is short (3 letters or less) make the word in the list lowercase if a word is long (7 letters or more) make the word in the list uppercase call the word_mixer function with the modified list print the return value from the word_mixer function word_mixer Function has 1 argument: an original list of string words, containing greater than 5 words and the function returns a new list. sort the original list create a new list Loop while the list is longer than 5 words: in each loop pop a word from the sorted original list and append to the new list pop the word 5th from the end of the list and append to the new list pop the first word in the list and append to the new list pop the last word in the list and append to the new list return the new list on exiting the loop Before: # [] create poem mixer # [] copy and paste in edX assignment page index = 0 user_input = input("Enter a saying or poem: ") words_list = user_input.split() words_new_list = [] new_list = [] while index < len(words_list): if len(words_list[index]) < 4: words_new_list.append(words_list[index].lower()) index += 1 elif len(words_list[index]) > 6: words_new_list.append(words_list[index].upper()) index += 1 else: words_new_list.append(words_list[index]) index += 1 def word_mixer(words_new_list): words_new_list.sort() length = len(words_new_list) while length > 5: new_list.append(words_new_list.pop(-5)) new_list.append(words_new_list.pop(0)) new_list.append(words_new_list.pop()+"\n") joined = " ".join(new_list) print(joined) test = word_mixer(words_new_list)After: index = 0 user_input = input("Enter a saying or poem: ") words_list = user_input.split() words_new_list = [] new_list = [] while index < len(words_list): if len(words_list[index]) < 4: words_new_list.append(words_list[index].lower()) index += 1 elif len(words_list[index]) > 6: words_new_list.append(words_list[index].upper()) index += 1 else: words_new_list.append(words_list[index]) index += 1 def word_mixer(words_new_list): words_new_list.sort() while len(words_new_list) > 5: new_list.append(words_new_list.pop(-5)) new_list.append(words_new_list.pop(0)) new_list.append(words_new_list.pop()+"\n") joined = " ".join(new_list) print(joined) test = word_mixer(words_new_list) RE: New to Python. Please help!!! - ThomasL - Jun-24-2019 Your 1st while loop 'while length > 5:' doesn´t stop if length > 5 as there is no change of length in the loop. In your 2nd code example the condition 'while len(words_new_list) > 5:' is evaluated each loop and as you are modifying the words_new_list the len() is different. |