Re: [Tutor] I'm having a small problem with my code
On 23/05/2019 13:16, David Lifschitz wrote: > The next job of the code is to sort the list of numbers that were inputted > in an ascending fashion. You are aware that Python lists have a built in sort method? It will be more efficient than anything you can create yourself in Python. Assuming you know that and decided to write a sort routine for fun > There is no error from the code, however, when I run the code the first > inputted number stays in its place and doesn't get sorted with the rest of > the numbers. > Any advice??? Yes, see below: > emptyList = [] This is a terrible name since it becomes misleading the instant you put anything into it! number_list or similar would be more accurate. Or even just 'data' > nOFN = int(input("how many numbers do you want to sort: ")) > > for x in range(nOFN): > number1 = int(input("input number: ")) > emptyList.append(number1) You could have used a list comprehension: emptyList = [int(input("input number: ")) for n in range(nOFN)] Now onto the problem sort code > firstElement = emptyList[0] Why did you do this? You never refer to firstElement again... > n = len(emptyList) > for j in range(1, n): > if emptyList[j-1] > emptyList[j]: > (emptyList[j-1], emptyList[j]) = (emptyList[j], emptyList[j-1]) Consider the first case, j=1 If the first element is greater than the second you swap them. Otherwise you leave them in place. The loop now considers elements 2 and 3. If 2 >3 you reverse them, otherwise move on. But if element 3 is less than element 1 you never go back to move it to the top. Consider this example - [3,2,1] 1st iteration -> 2,3,1 2nd iteration -> 2,1,3 Loop ends. But you never swapped 1 and 2 after(or during) the last iteration. Your sort routine is fundamentally flawed. You need a rethink. But not too much because the built in sort will nearly always be preferred! Incidentally, creating working sort algorithms is one of the hardest things to get right in computing. It is one of those things that can seem right then one specific pattern will break it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm having a small problem with my code
On 23May2019 15:16, David Lifschitz wrote: I am currently working on a project for myself. The code posted here is supposed to ask the user for an amount of numbers, what those numbers are, and places those numbers in a list. The next job of the code is to sort the list of numbers that were inputted in an ascending fashion. There is no error from the code, Regardless, it clearly isn't doing what you want. In this situation it is helpful (to us, and therefore to you later) to post the output from a run and an example of the output you wanted, and and description of what is deficient in the programme output. You've got the last 2 of these 3. however, when I run the code the first inputted number stays in its place and doesn't get sorted with the rest of the numbers. Comments below the code. emptyList = [] nOFN = int(input("how many numbers do you want to sort: ")) for x in range(nOFN): number1 = int(input("input number: ")) emptyList.append(number1) firstElement = emptyList[0] n = len(emptyList) for j in range(1, n): if emptyList[j-1] > emptyList[j]: (emptyList[j-1], emptyList[j]) = (emptyList[j], emptyList[j-1]) print(emptyList) Well, you only make one pass over the list. So if emptyList[0] <= emptyList[1] _on the first (and only) pass_, it will not move. Try comparing: 2 5 3 and: 3 2 5 and see if the behaviour differs. BTW, what you['re implementing looks like a bubble sort to me (look it up). But a since pass over the list isn't enough to sort the whole thing. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] I'm having a small problem with my code
Hi. I am currently working on a project for myself. The code posted here is supposed to ask the user for an amount of numbers, what those numbers are, and places those numbers in a list. The next job of the code is to sort the list of numbers that were inputted in an ascending fashion. There is no error from the code, however, when I run the code the first inputted number stays in its place and doesn't get sorted with the rest of the numbers. Any advice??? emptyList = [] nOFN = int(input("how many numbers do you want to sort: ")) for x in range(nOFN): number1 = int(input("input number: ")) emptyList.append(number1) firstElement = emptyList[0] n = len(emptyList) for j in range(1, n): if emptyList[j-1] > emptyList[j]: (emptyList[j-1], emptyList[j]) = (emptyList[j], emptyList[j-1]) print(emptyList) Sent from an email account ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Learning something new.
On 23/05/2019 08:38, Jessica Rabbit wrote: > Hello, my name is Jessica and I hope I found the right e mail to help me > learn of to write in code and eventually learn to hack. Hi Jessica and welcome to the tutor list. > know how to do something that my father can. If this email is read by > someone who is willing to help, *teach*, and *yell* at me (figuratively) > please let me know. The way the list works is that people ask questions of the list membership, who then answer them. The more specific the question the more specific will be the answer. Include any code you have written and any error messages in the message body, do not use attachments, especially not binary ones like screenshots, since the server will drop them. It also sometimes helps to know your Python version and your OS. Have fun, we look forward to your questions and we try not to yell at anyone. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor