这是我的代码
#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)
我该如何使用if条件和要再次输入的新值使该代码再次运行,或者只需要放置一个if条件并在其中复制/粘贴我的代码?
答案 0 :(得分:0)
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)
这将永远循环,因此您需要确定结束条件,并将while True
替换为在某些时候可能变为错误的内容
答案 1 :(得分:0)
我会尝试这样的事情:
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)
然后,每次您要调用它时,都可以仅调用nth_term()
。如果您想多次调用它,可以将其放在这样的循环中:
for each in range(5):
nth_term()