我需要我的程式顯示一個串列,然后詢問用戶是否要添加任何內容;然后它將該輸入添加到串列中。然后它在單獨的輸入中再次詢問用戶是否要添加其他任何內容,如果他們按 Enter 鍵,它會列印包括所有新輸入的串列并結束大的 while 回圈。
list1 = [1,2,3,4,5,6]
def list_adder(liist):
print("Here is the list:\n")
def show_list():
for element in liist:
print(element)
show_list()
x = True
while x == True:
counter = 0
if counter == 0:
add_input1 = input("\nWhat would you like to add:\n")
liist.append(add_input1)
counter 1
while counter == 1:
add_input2 = input("\nWhat else would you like to add to the list?: \n")
to_do_list.append(add_input2)
if not add_input2:
show_list()
counter = 1
x == False
list_adder(list1)
我試過了,但它一遍又一遍地說“你想添加什么”
uj5u.com熱心網友回復:
制作counter = counter 1并嘗試重新運行代碼。
另外,請更改x == False為x = False
會有其他與to_do_list未定義相關的錯誤。我相信您需要將其更改為liist.
下面的代碼應該可以滿足您的要求:
list1 = [1,2,3,4,5,6]
def list_adder(liist):
print("Here is the list:\n")
def show_list():
for element in liist:
print(element)
show_list()
x = True
while x == True:
counter = 0
if counter == 0:
add_input1 = input("\nWhat would you like to add:\n")
liist.append(add_input1)
counter = counter 1
while counter == 1:
add_input2 = input("\nWhat else would you like to add to the list?: \n")
liist.append(add_input2)
if not add_input2:
show_list()
counter = 1
x = False
uj5u.com熱心網友回復:
需要將遞增的計數器變數分配回自身,如下所示。
if counter == 0:
add_input1 = input("\nWhat would you like to add:\n")
liist.append(add_input1)
counter = 1
uj5u.com熱心網友回復:
counter 1在 下洗掉liist.append(add_input1),因為我已將條件從 更改while counter == 1為while counter == 0。該行有一個小錯誤list1.append(add_input2),您寫了一些無效的名稱,因此 Python 會引發錯誤。不要這樣做x == False,您是在比較值,而不是分配它們。而是做x = False,你分配x給假的地方。
list1 = [1,2,3,4,5,6]
def list_adder(liist):
print("Here is the list:\n")
def show_list():
for element in liist:
print(element)
show_list()
x = True
while x == True:
counter = 0
if counter == 0:
add_input1 = input("\nWhat would you like to add:\n")
liist.append(add_input1)
while counter == 0:
add_input2 = input("\nWhat else would you like to add to the list?: \n")
list1.append(add_input2)
if not add_input2:
print(show_list())
counter = 1
x = False
list_adder(list1)
uj5u.com熱心網友回復:
當前代碼中的問題:
您當前的方法存在幾個問題。這些如下:
counter 1計算一個新值但不改變counter變數的值。例如 ifcounteris0thencounter 1會給你值1,但變數counter仍然是0. 要更正此問題,您應該使用counter = 1.看來您的串列將僅存盤整數值。如果是這種情況,則應使用該
int()函式將從用戶讀取的值轉換為整數值。該變數
to_do_list在使用前未定義,會導致錯誤。我假設你的意思是liist.while x == True可以替換為while x。x == Falsex = False如果您希望將變數設定x為 value應該是False。該代碼包含一些不必要的嵌套。例如,您可以洗掉最里面的 while 回圈,而是使用一個具有適當邏輯的回圈來實作相同的結果。
使用
counter變數的另一種方法是簡單地檢查串列大小是否已更改。
改進的解決方案:
下面的解決方案解決了上述問題,并演示了如何簡化解決方案:
def append_list(the_list):
initial_size = len(the_list)
print(f"The list currently contains {initial_size} values as follows: \n{the_list}")
while True:
variation = "would" if len(the_list) == initial_size else "else would"
value = input(f"What {variation} you like to add?")
if value:
the_list.append(int(value))
else:
print(f"The final list contains {len(the_list)} values as follows: \n{the_list}")
return
user_list = [1, 2, 3, 4, 5, 6]
append_list(user_list)
附加資訊:
如果您想在新行上列印串列中的每個值,則可以執行以下操作:
print(*the_list, sep="\n")
上面的陳述句將解包串列,然后使用新的行分隔符列印每個專案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/532026.html
上一篇:第一百零八篇:最常用的基本資料型別(Number型別)
下一篇:串列未在Python的函式中定義
