如何取新号和运行一样的过程python
How to take a new number and run the same process python
我正在尝试编写一个脚本来计算著名的“3x+1”等式,我希望 python 由用户输入一个数字,然后确定它是偶数还是奇数。如果偶数除以一半,如果奇数则做 3x+1,然后取经过这个过程的新数字,再做一次,直到数字变成 4。
除了采用新编号并重复该过程的部分外,我已完成所有部分并正常工作。有谁知道我怎样才能得到这个来获取创建的新号码并重复这个过程。
(对于那些想知道为什么我做了这么多次的人,我想不出一个名字,我的猫就在我旁边喵喵叫吃东西,所以我就跟着它去了)
代码:
meow = 0
num = int(input("Enter a number"))
meow += 1
while True:
if num == 4:
print("you are now caught in a loop. (4 becomes 2 which becomes 1 which becomes 4 ect)")
print("it took this number",meow-1,"times to get caught in this uninevitable loop for all recorded numbers")
else:
if(num % 2 != 0):
print(num*3+1)
else:
print(num/2)
只需更新 num
每次迭代:
meow = 0
num = int(input("Enter a number"))
meow += 1
while True:
if num == 4:
print("you are now caught in a loop. (4 becomes 2 which becomes 1 which becomes 4 ect)")
print("it took this number",meow-1,"times to get caught in this uninevitable loop for all recorded numbers")
else:
if(num % 2 != 0):
num = num*3+1
print(num)
else:
num = num/2
print(num)
我正在尝试编写一个脚本来计算著名的“3x+1”等式,我希望 python 由用户输入一个数字,然后确定它是偶数还是奇数。如果偶数除以一半,如果奇数则做 3x+1,然后取经过这个过程的新数字,再做一次,直到数字变成 4。 除了采用新编号并重复该过程的部分外,我已完成所有部分并正常工作。有谁知道我怎样才能得到这个来获取创建的新号码并重复这个过程。 (对于那些想知道为什么我做了这么多次的人,我想不出一个名字,我的猫就在我旁边喵喵叫吃东西,所以我就跟着它去了) 代码:
meow = 0
num = int(input("Enter a number"))
meow += 1
while True:
if num == 4:
print("you are now caught in a loop. (4 becomes 2 which becomes 1 which becomes 4 ect)")
print("it took this number",meow-1,"times to get caught in this uninevitable loop for all recorded numbers")
else:
if(num % 2 != 0):
print(num*3+1)
else:
print(num/2)
只需更新 num
每次迭代:
meow = 0
num = int(input("Enter a number"))
meow += 1
while True:
if num == 4:
print("you are now caught in a loop. (4 becomes 2 which becomes 1 which becomes 4 ect)")
print("it took this number",meow-1,"times to get caught in this uninevitable loop for all recorded numbers")
else:
if(num % 2 != 0):
num = num*3+1
print(num)
else:
num = num/2
print(num)