Python 带“for loop”的3n+1序列
这是我写的代码Python 带“for loop”的3n+1序列,python,python-3.x,Python,Python 3.x,这是我写的代码 def seq3np1(n): count = 0 while(n != 1): if(n % 2) == 0: # n is even n = n // 2 count += 1 else: # n is odd n = n * 3 + 1 count += 1 return
def seq3np1(n):
count = 0
while(n != 1):
if(n % 2) == 0: # n is even
n = n // 2
count += 1
else: # n is odd
n = n * 3 + 1
count += 1
return count
def main():
num = int(input("what number do you want to put?: "))
start = int(input("Number for upper bound: "))
for i in range(1, start+1):
count = seq3np1(num)
print("This is the starting number: ", num)
print("Number of iterations:", count)
main()
我需要做的是:
•要求用户提供用于范围上限的值
•使用名为start的迭代变量创建for循环,该变量提供从1到用户提供的上限的值
•为每个start值调用seq3np1函数一次
•编写打印语句,打印开始值和迭代次数
但是我的函数并不是对start的每个值都调用seq3np1函数
我的for循环函数是否做错了什么
此外,我还需要创建图形数据。但是,我应该在主函数中使用setworldcoordinates函数吗?还是要创建另一个用于绘图的函数?您只需在for循环中打印计数,并调用count=seq3np1i而不是seq3np1num
def main():
start = int(input("Number for upper bound: "))
for i in range(1, start+1):
count = seq3np1(i)
print("Number of iterations:", count)