Python Forum

Full Version: question about loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Sorry I am quite new in python, I have a question and i was wondering if anyone can help me?

question below:
Create a function called Q9 that takes an integer argument and uses a loop to print each number in the range of the argument. For example: 5 would print: 0 1 2 3 4

my code is below:
def Q9():
    for i in range(9):
        print(i)
Q9()
which is wrong, they wanted a answer with Q9(9), i am confuse where I can input this function.
You're almost there. What you've failed to do (because you're new to Python, for which you don't need to be sorry) is to pass an argument to your function.

def Q9(n): # n is an arbitrary variable name
    for i in range(n):
        print(i)

Q9(5)
That will produce an output of:

Output:
0 1 2 3 4
Now, it is possible to produce:

Output:
0 1 2 3 4
... with a simple alteration to the print() function. It's your task to look into what options you can use with said function and post back your solution.

Hint: >>> help(print)
thank you so much appreciated !!!! :) makes sense
(Nov-05-2022, 08:03 AM)rpang Wrote: [ -> ]thank you so much appreciated !!!! :) makes sense
def Q9(n): # n is an arbitrary variable name
    for i in range(n):
        print(i)
Q9(9)
looks like this code print the answer twice? is it because of the print(i) print a separate set of numbers?
Post your code, like this:

[python] # your code here [/python]

... and your output, like this...

[output]your output here[/output]
(Nov-05-2022, 08:31 AM)rob101 Wrote: [ -> ]Post your code, like this:

[python] # your code here [/python]

... and your output, like this...

[output]your output here[/output]

I am confuse i am using Jupiter, seems like we have 2 print print(i) and Q9(5) ? not sure why it error on 2 print answer.


def Q9(n): # n is an arbitrary variable name
    for i in range(n):
        print(i)
 
Q9(5)
From your first post:

(Nov-05-2022, 07:19 AM)rpang Wrote: [ -> ]question below:
Create a function called Q9 that takes an integer argument and uses a loop to print each number in the range of the argument. For example: 5 would print: 0 1 2 3 4

I'm also confused, because aside from the output not being on the same line (a fact that I covered in my first post and in which I gave you a hint on how to fix that) the script fulfills the objective.
There is one print, the print in the function named Q9().
def Q9(n):
    for i in range(n):
        print(i)
Q9(5)
Output:
0 1 2 3 4
As rob101 said, you can modify the print command to put all the output on 1 line.
def Q9(n): # n is an arbitrary variable name
    for i in range(n):
        print(i, end=" ")  # This prints numbers
Q9(5)  # This calls the function that prints the numbers
Output:
0 1 2 3 4
jIn both instances there is 1 print for each number counting up from 0 to n. It is just a matter of printing all the numbers on one line or separate lines.

If that doesn't resolve your confusion, there is one other thing that may be producing extra output in a jupyter notebook.

Jupyter is a bit odd in that it automatically prints results without having to call print(). If you call a function and it returns a result that is not None, jupyter automatically prints the result. I modified Q(n) to return n. Jupyter will see a non-None result and print it. This code will print the numbers, then it prints n.
def Q9(n):
    for i in range(n):
        print(i, end=" ")
    return(n)
Q9(5)
Output:
0 1 2 3 4 5
The automatic printing can be suppressed by adding a semicolon to the end of the line.
def Q9(n):
    for i in range(n):
        print(i, end=" ")
    return(n)
Q9(5);
If you are seeing extra prints that you don't expect, look for function calls that return values. To stop them from printing, add semicolons to those lines.