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?
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
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.