我正在嘗試制作一個計算著名的“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)
uj5u.com熱心網友回復:
只需更新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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/408846.html
標籤:
