LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-17-2022, 03:50 AM   #1
Alexmicu
LQ Newbie
 
Registered: Feb 2022
Posts: 15

Rep: Reputation: 0
python not working when return even numbers


I try to write below code in Ubuntu. i want to return 5 even number which is greater than 3, idealy it should get the list of 4,6,8,10,12. but it
only return 4,6,8,10. anything wrong? thanks.
def evennumber(m,n):
st = []
for i in range(m-1,n+1):
st.append(2*i)
for x in range(len(st)):
print (st[x])


if __name__=='__main__':
evennumber(3,5)
 
Old 10-17-2022, 03:56 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
you ought to use code tags, otherwise the python script is actually unreadable.
 
Old 10-17-2022, 04:02 AM   #3
Alexmicu
LQ Newbie
 
Registered: Feb 2022
Posts: 15

Original Poster
Rep: Reputation: 0
yes, i use correct indention, but result is wrong. ii believe something else is wrong
 
Old 10-17-2022, 04:35 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Quote:
Originally Posted by Alexmicu View Post
yes, i use correct indention, but result is wrong. ii believe something else is wrong
You need to use code tags. Edit your original post and insert them. That will fix this indentation issue.
 
Old 10-17-2022, 04:43 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,730

Rep: Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920
Look at how the range function actually works.
https://www.w3schools.com/python/ref_func_range.asp
 
Old 10-17-2022, 08:19 AM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,614

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554
Quote:
Originally Posted by Alexmicu View Post
i want to return 5 even number which is greater than 3, idealy it should get the list of 4,6,8,10,12. but it
only return 4,6,8,10. anything wrong?
Since this is clearly a homework/learning question, I'll provide the following pointers:

The simpler a piece of code is, the less likely it is to go wrong, (and the easier it is to find when something does go wrong). Think about your current code and see if there any ways you can come up with to reduce the amount of code you have, in order to achieve the same thing in fewer steps, and help narrow down which part isn't behaving as you expected.

With that in mind, take a closer look at the documentation for range.

Also, remember, the print function accepts all types of objects, and can even accept multiple arguments.

A temporary print statement can be a useful way to debug things (i.e. understand what the code is actually doing). For example, see if there are any places where putting "print('i',i)" helps you understand what is happening.

There is a lot of helpful documentation at docs.python.org, especially the Library and Language references. The official documentation is almost always the best place to start.


Quote:
Originally Posted by Alexmicu View Post
yes, i use correct indention, but result is wrong. ii believe something else is wrong
When posting on forums, you need to use "[code]..[/code]" tags to preserve formatting.

On your first post you will see an "Edit" button in the bottom right corner (between "Report" and "Quote" - use that and you can ensure the forum displays the indentation as you provided it, which helps other users read your code. (The easier it is for people to read code and answer questions, the more likely people will do so.)

 
Old 10-17-2022, 10:56 AM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,141
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
Quote:
i want to return 5 even number which is greater than 3
Using your example:
Code:
#!/usr/bin/python

def evennumber(m,n):
    st = []
    for i in range(m, n):
        if (i % 2) == 0 and (i > 3) and (i < 14):
            st.append(i)
    for x in st:
        print (x)
        
if __name__=='__main__':
    evennumber(1 , 20)
 
Old 10-22-2022, 05:35 PM   #8
xlfs-0.2
Member
 
Registered: Oct 2022
Posts: 207

Rep: Reputation: 44
To build linux from scratch one needs 3 or 4 Python installed and to juggle files so programs know which is installed. And it's getting worse.

Simple programs written in python2 cannot print() when python3 is installed. The languages continually mutates so that old code no longer works.
 
Old 10-24-2022, 07:31 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
@xlfs-0.2 This thread is not about building LFS, and no-one has said anything about Python 2. Your "contribution" is worthless. Go away.

(And for your edification: that program works the same in both Python 2 and 3).

OP:

Let me a assume you formatted it like this:

Code:
def evennumber(m,n):
  st = []
  for i in range(m - 1, n + 1):
    st.append(2 * i)
  for x in range(len(st)):
    print (st[x])

if __name__=='__main__':
  evennumber(3,5)
The first range would be range(2, 6). Which would be 2, 3, 4, 5. The first argument to range is considered inclusive and the last would be considered exclusive. If I recall my math correctly: [2, 6). See the problem?

Last edited by dugan; 10-24-2022 at 07:34 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can read from signalfd return data in not-multiple of sizeof(signalfd_siginfo)? Can read return zero? yvw Linux - Kernel 0 02-22-2022 07:45 AM
Why can I not be able to type in any numbers after I signed in my passwords that does numbers? mech928 Linux - Newbie 17 08-11-2020 08:14 AM
Kprobe return callback not called on function return bogdanul2003 Linux - Kernel 1 03-16-2017 11:17 AM
Return from root to user using python os.system('kill') not working bobdxcool Linux - Software 1 09-22-2016 02:26 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration