Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about loops
#1
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.
Yoriz write Nov-05-2022, 08:02 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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)
Yoriz and rpang like this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
thank you so much appreciated !!!! :) makes sense
rob101 likes this post
Reply
#4
(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?
Yoriz write Nov-05-2022, 08:34 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#5
Post your code, like this:

[python] # your code here [/python]

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

[output]your output here[/output]
rpang likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
(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)
Yoriz write Nov-05-2022, 11:15 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#7
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework help:While Loops question Midhat_School 6 3,125 Jul-26-2020, 10:23 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

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