Python Forum
Python Homework help plz (Python only printing None)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Homework help plz (Python only printing None)
#1
list=['the','i','am','light','life']
a=sorting(list)

def sorting(x):
    for i in range(0,4):
        if len(x[i]) < len(x[i+1]):
            pass
        else:
            temp=x[i+1]
            x[i+1]=x[i]
            x[i]=temp

print(a)
The home work is to sort those words by the length of the word.
the coding must use the bottom part codes


list=['the','i','am','light','life']
a=sorting(list)

print(a)
my home work is to make the coding for def sorting

any help plz? whats wrong and why is this happening?
Reply
#2
list=['the','i','am','light','life']
a=sorting(list)

def sorting(x):
    for i in range(0,4):
        if len(x[i]) < len(x[i+1]):
            pass
        else:
            temp=x[i+1]
            x[i+1]=x[i]
            x[i]=temp

print(a)
This coding prints None.
I believe i need to put in "return" somewhere inside the "def sorting(x)"
Can anyone help?
Reply
#3
What do you expect the function to do? If you give it a list of things, what would you want it to give you back?

Also, here's the relevant part of the docs if you'd like to read up on it instead: https://docs.python.org/3/tutorial/contr...-functions
Reply
#4
your function does not return anything, so by default it returns None. So when you call it on line 2, value of a becomes None. Then you print a.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Also, a pass statement is almost always unnecessary
word_list=['the','i','am','light','life']
a=sorting(word_list)
 
def sorting(x):
    for i in range(1, len(word_list)):
        if len(x[i-1]) > len(x[i]):
            temp=x[i+1]
            x[i+1]=x[i]
            x[i]=temp
Reply
#6
(Dec-04-2018, 05:52 PM)chubsie Wrote: I believe i need to put in "return" somewhere inside the "def sorting(x)"

It belongs at the end. And trying playing around with x, y = y, x.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework Python unit test Paragoon2 4 1,478 Dec-12-2022, 12:45 PM
Last Post: Paragoon2
  HELP in python homework makashito 4 3,864 Oct-12-2021, 10:12 AM
Last Post: buran
Exclamation Python Homework (Urgent help needed!!) chickenseizuresalad 6 4,217 Oct-11-2021, 01:59 AM
Last Post: Underscore
  CyperSecurity Using Python HomeWork ward1995 1 1,931 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,257 May-09-2021, 11:19 AM
Last Post: Yoriz
  HomeWork Python - Drawing window with text center. Voraman 8 3,212 Jan-09-2021, 06:53 PM
Last Post: Voraman
  [TkInter]Simple Python Homework zaji_123 4 4,010 Jan-01-2021, 07:09 PM
Last Post: EliesBe
  Homework with python Johnsonmfw 1 1,660 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Applied Data Science with Python - homework 2.2 (Weather plotting) eyavuz21 4 3,327 Jun-03-2020, 07:09 PM
Last Post: eyavuz21
  Python Homework Help *Urgent GS31 2 2,539 Nov-24-2019, 01:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020