0

I am trying to make a simple loop where it prints the output numbers 0-9 on separate lines. What am I doing wrong? I have looked at the examples on here and they don't really help me. If you could explain where I went wrong that would be very helpful.

def singleline(item):
    number = 0
    while number < 10:
        print(number)
        number = number + 1 
7
  • what are you doing with the item parameter ? and where's your main program ? Commented Nov 11, 2014 at 23:30
  • What is your actual problem? Is it that you don't see the numbers? Or is it printing something weird? Commented Nov 11, 2014 at 23:33
  • I dont see anything. I just want to print numbers 0,1,2,3,4,5,6,7,8,9 all on separate lines Commented Nov 11, 2014 at 23:37
  • Ok, then try what tdelaney had. Also, is this part of a larger project? (Please tag me in your answer, otherwise I won't see it.) Commented Nov 11, 2014 at 23:43
  • @AHuman no that is all I am trying to do. What you see is my only lines of code. I can't get it to print anything out what so ever. Commented Nov 11, 2014 at 23:47

4 Answers 4

5

You've defined a function but you haven't called it. Just add

singleline(1)

to the end of the script.

Sign up to request clarification or add additional context in comments.

Comments

2

Try using a for loop with range.

for num in range(10):
    print(num)

This is more concise than using a while loop.

Also, if you are using a while loop, I would recommend using number+=1. It is the same as number=number+1, but just looks cleaner.

2 Comments

There should be a colon at the end of the for loop. Considering that the function is called 'singleline' and takes an 'item', I think we are seeing experimental code and it may be premature to guess that the for loop is any better than the real code.
@tdelaney Good point. However, the two lines the for loop uses look much cleaner than the while.
0

Firstly remember to call the function at the end of your otherwise you have just executed it singleline(). Also you haven't used the item you put into the parameters.

A better way to write this using a while loop would be. def singleline(): num = 0 while num < 10: print(num) num += num The += just means add one to of the variable on the left to the variable on the right. For example a = 1 b = 2 a += b a would not equal 3 because it adds b to it's original value. However if you wanted something more efficient you could use this: for num in range(10): print(num) for loops work in the same way as a while loop (take a condition and do content until stopped) but does it for the amount of times set. So in simple terms all this code means is print num plus 1.

Comments

0

Woah, your code is way too complicated -

for x in range (0, 10):
  print x 

Should work perfectly (python), Good Luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.