I want to apply loop but i don't know how to apply it on my code
up vote
0
down vote
favorite
here's my code
#Program for calculating the nTH Term
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
how can i make this code to run again using if condition and with new values which i will input again ORrr i just have to put an if condition and copy/paste my code inside it?
python loops
add a comment |
up vote
0
down vote
favorite
here's my code
#Program for calculating the nTH Term
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
how can i make this code to run again using if condition and with new values which i will input again ORrr i just have to put an if condition and copy/paste my code inside it?
python loops
1
inside a while loop?
– mad_
Nov 8 at 16:53
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
here's my code
#Program for calculating the nTH Term
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
how can i make this code to run again using if condition and with new values which i will input again ORrr i just have to put an if condition and copy/paste my code inside it?
python loops
here's my code
#Program for calculating the nTH Term
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
how can i make this code to run again using if condition and with new values which i will input again ORrr i just have to put an if condition and copy/paste my code inside it?
python loops
python loops
edited Nov 8 at 16:54
asked Nov 8 at 16:52
AbdurRafay
112
112
1
inside a while loop?
– mad_
Nov 8 at 16:53
add a comment |
1
inside a while loop?
– mad_
Nov 8 at 16:53
1
1
inside a while loop?
– mad_
Nov 8 at 16:53
inside a while loop?
– mad_
Nov 8 at 16:53
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
while True:
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point
add a comment |
up vote
0
down vote
I would try something like this:
def inputs():
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
return (a, d)
def nth_term():
a, d = inputs()
sequence=range(0,40,3)
n=len(sequence)
print()
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:
for each in range(5):
nth_term()
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
while True:
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point
add a comment |
up vote
0
down vote
accepted
while True:
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
while True:
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point
while True:
sequence=range(0,40,3)
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
n=len(sequence)
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
This will loop forever, so you need to decide on a end condition and replace while True with something that can become false at some point
answered Nov 8 at 16:58
Karl
1,85842951
1,85842951
add a comment |
add a comment |
up vote
0
down vote
I would try something like this:
def inputs():
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
return (a, d)
def nth_term():
a, d = inputs()
sequence=range(0,40,3)
n=len(sequence)
print()
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:
for each in range(5):
nth_term()
add a comment |
up vote
0
down vote
I would try something like this:
def inputs():
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
return (a, d)
def nth_term():
a, d = inputs()
sequence=range(0,40,3)
n=len(sequence)
print()
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:
for each in range(5):
nth_term()
add a comment |
up vote
0
down vote
up vote
0
down vote
I would try something like this:
def inputs():
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
return (a, d)
def nth_term():
a, d = inputs()
sequence=range(0,40,3)
n=len(sequence)
print()
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:
for each in range(5):
nth_term()
I would try something like this:
def inputs():
a=int(input("The first term is:"))
d=int(input("The common difference is:"))
return (a, d)
def nth_term():
a, d = inputs()
sequence=range(0,40,3)
n=len(sequence)
print()
print("The number of terms is:",n)
print("The last term of sequence is: ")
Tn=a+((n-1)*d)
print(Tn)
Then each time you want it to be called you can just call nth_term(). If you wanted to call it a certain number of times you could put it in a loop like this:
for each in range(5):
nth_term()
answered Nov 8 at 17:12
John Meade
444
444
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212502%2fi-want-to-apply-loop-but-i-dont-know-how-to-apply-it-on-my-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
inside a while loop?
– mad_
Nov 8 at 16:53