new to python and I have this assingment: I need to write a recursive function that takes paramaters (aString,nTimes) and it needs to print the aString parameter the number of times specified by nTimes. I cannot use a for or while loop, just recursion. How would I go about doing this? all i know how to do is get it started and use recursion to call function again.
def printNtimes(aString,nTimes):
I also know how to do it using recursion with one parameter: -
def n_lines(n):
-
if n>0:
-
print "Line"
-
n_lines(n-1)
2 1700
new to python and I have this assingment: I need to write a recursive function that takes paramaters (aString,nTimes) and it needs to print the aString parameter the number of times specified by nTimes. I cannot use a for or while loop, just recursion. How would I go about doing this? all i know how to do is get it started and use recursion to call function again.
def printNtimes(aString,nTimes):
I also know how to do it using recursion with one parameter: -
def n_lines(n):
-
if n>0:
-
print "Line"
-
n_lines(n-1)
It's just the same; no big change: - def n_lines(anStr, n):
-
if n>0:
-
print anStr
-
n_lines(anStr, n-1)
-
-
-
n_lines("hello", 5)
To learn how to do the pretty printing (which I've added to your post), please read the "REPLY GUIDELINE" on the right hand side of the page while you reply. They are required under the rules of our Posting Guidelines. Thanks
bvdet 2,851
Expert Mod 2GB
It's just the same; no big change: - def n_lines(anStr, n):
-
if n>0:
-
print anStr
-
n_lines(anStr, n-1)
-
-
-
n_lines("hello", 5)
To learn how to do the pretty printing (which I've added to your post), please read the "REPLY GUIDELINE" on the right hand side of the page while you reply. They are required under the rules of our Posting Guidelines. Thanks
A very minor change: - def n_lines(anStr, n):
-
if n:
-
print anStr
-
n_lines(anStr, n-1)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: hokiegal99 |
last post by:
A few questions about the following code. How would I "wrap" this in a
function, and do I need to?
Also, how can I make the code smart enough to realize that when a file
has 2 or more bad...
|
by: da Vinci |
last post by:
Greetings.
I want to get everyone's opinion on the use of recursion.
We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted...
|
by: Alexis |
last post by:
Hi,
I need to transform one xml document into a second xml document. They
both have many nodes so xslt works fine, but there is one node I have
not figure out how to transform.
Here it is:...
|
by: Ken |
last post by:
Hello,
I have a recursive Sierpinski code here.
The code is right and every line works fine by itself. I wish for all of
them to call the function DrawSierpinski. But in this cae it only calls...
|
by: Sathyaish |
last post by:
Can every problem that has an iterative solution also be expressed in
terms of a recursive solution?
I tried one example, and am in the process of trying out more examples,
increasing their...
|
by: MTD |
last post by:
Hello all,
I've been messing about for fun creating a trial division factorizing
function and I'm naturally interested in optimising it as much as
possible.
I've been told that iteration in...
|
by: NOO Recursion |
last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a
thing called a "blob". A blob in the grid is made up of asterisks. A blob
contains at least one asterisk. If an...
|
by: Mumia W. |
last post by:
Hello all.
I have a C++ program that can count the YOYOs that are in a grid of
Y's and O's. For example, this
Y O Y O O Y
O Y O Y O O
Y O Y Y O Y
O Y O O Y O
O Y Y O Y O
|
by: Muzammil |
last post by:
int harmonic(int n) {
if (n=1) {
return 1;
}
else {
return harmonic(n-1)+1/n;
}
}
can any help me ??
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |