所以基本上我想要實作的是讓我的程式對一個輸入變數執行多次迭代(我為此使用了一個 for 回圈)。在 for 回圈結束后,程式會詢問用戶是否要繼續,如果是,則程式會要求用戶再進行 n 次迭代以對相同的輸入變數執行。然后程式必須從它離開前一個值的位置開始操作,我為此使用了一個字典,但找不到它作業。如果我能得到一些幫助會很棒。我的代碼粘貼在下面:
def function(x, i):
return x**i
num_dic = {}
j = 1
while True:
if j == 1:
start = 1
n = int(input("number of iterations: "))
x = int(input("Number to perform operation: "))
for i in range(start, n 1):
total = function(x, i)
print(f"{x}", "**", i, "=", total)
num_dic[f"{i}"] = total
elif j > 1:
start = int(sorted(num_dic.keys())[-1]) 1
x = sorted(num_dic.values())[-1]
n = int(input("number of iterations: "))
for i in range(start, n 1):
total = function(x, i)
print(f"{x}", "**", i, "=", total)
num_dic[f"{i}"] = total
j = 1
while True: # while loop for repeating program
ask = input("Do you want to continue?(Y to continue/ N to exit): ")
if ask.upper() == "Y":
break
if ask.upper() == "N":
break
else:
print("Please enter a correct operation (Y/N) ")
continue
if ask.upper() == "Y":
continue
else:
break
我得到的當前輸出:
number of iterations: 5
Number to perform operation: 2
2 ** 1 = 2
2 ** 2 = 4
2 ** 3 = 8
2 ** 4 = 16
2 ** 5 = 32
Do you want to continue?(Y to continue/ N to exit): y
number of iterations: 5
在這部分之后,它什么都不做。
所需的輸出應如下所示:
number of iterations: 5
Number to perform operation: 2
2 ** 1 = 2
2 ** 2 = 4
2 ** 3 = 8
2 ** 4 = 16
2 ** 5 = 32
Do you want to continue?(Y to continue/ N to exit): y
number of iterations: 5
2 ** 6 = 64
2 ** 7 = 128
2 ** 8 = 256
2 ** 9 = 512
2 ** 10 = 1024
uj5u.com熱心網友回復:
此操作不需要字典。您只需要一個干凈且適當的 while 回圈來跟蹤您的變數,特別是exponent
(j
原始代碼中的變數)和您的迭代次數,n
.
base
不會改變,所以它應該被留在while回圈之外,正如@Blckknght 對這個問題的回答所建議的那樣。
exponent = 1
base = int(input("Number to perform operation: "))
n = None # initialize n to None
while True:
# logic to keep track of n in the previous loop, if there is one.
if n:
n = int(input("number of iterations: ")) # cumulatively increment n if user wishes to continue.
else:
n = int(input("number of iterations: "))
# while-loop that prints the output for a single set of x and n values
while n >= j:
print(f"{base} ** {exponent} = {base ** exponent}")
exponent = 1
# get user input for continuation
ask = input("Do you want to continue?(Y/ N): ")
# while loop to handle user input values
while ask.upper() not in ["Y", "N"]:
ask = input("Please enter a correct operation (Y/N).")
if ask.upper() == 'Y':
continue # loop back to the outer while-loop and ask for another set of user input
elif ask.upper() == 'N':
break # break if user says no
uj5u.com熱心網友回復:
由于您只需要跟蹤您離開的位置,因此無需在字典中存盤許多值。只需記住基數和使用的最后一個指數就足夠了。并且方便的是,您使用的變數x
和i
您正在使用的變數不會在迭代之間自動重置,因此您可以從以下位置開始下一次迭代i 1
:
elif j > 1:
n = int(input("number of iterations: "))
for i in range(i 1, i n 1):
total = function(x, i)
print(f"{x}", "**", i, "=", total)
實際上,您可能可以簡化一些事情,以便第一次迭代不需要特殊情況。只需在回圈開始之前詢問基地!這是我的做法(也使用更具描述性的變數名):
base = int(input("Number to perform operation: "))
start_from = 1
while True:
n = int(input("number of iterations: "))
for exponent in range(start_from, start_from n):
print(f'{base} ** {exponent} = {function(base, exponent)}')
start_from = start_from n
# ask about continuing down here, optionally break out
如果您真的想首先提示迭代次數,在詢問基數之前,您可能可以通過使用哨兵值None
來完成這項作業,例如指示它何時尚未設定,以便您只提示它第一次迭代(沒有明確計算):
base = None
start_from = 0
while True:
n = int(input("number of iterations: "))
if base is None:
base = int(input("Number to perform operation: "))
# ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491070.html
上一篇:如何在每100次迭代后添加time.sleep()?
下一篇:在for回圈中重置索引