Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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])
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
yes, i use correct indention, but result is wrong. ii believe something else is wrong
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.)
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)
@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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.