簡體   English   中英

如何在列表中拆分整數?

[英]How do I split integers in a list?

所以我聽起來像菜鳥,但我不知道如何在列表中拆分整數,但是列表保留在變量中,用戶當前可以輸入自己的數字,這就是我所擁有的。任何錯誤

num = []
item = input(str("Please enter a series of numbers separated by a space").split())
[int(digit) for digit in str(item)]
num.append(item)
print(num)

quit_program = input("Would you like to print the average out or quit the program?") 
#Type "average" for the average however if you want to quit then type in "quit"

if quit_program == "quit":
  quit()
elif quit_program == "average":
  sum(num)/len(num)

如果我了解您要嘗試做的事情,那么您的分裂就在錯誤的地方。 您想分割輸入字符串,而不是提示符。

>>> inp = raw_input("please enter a series of numbers separated by a space\n")
please enter a series of numbers separated by a space 
3 4 5
>>> nums = [ int(i) for i in inp.split()]
>>> nums
[3, 4, 5]

如果您只是想將輸入作為一個用空格分隔的字符串,那么您也將要使用raw_input(請參閱此處: http : //www.python-course.eu/input.php )。 然后,您將需要將該轉換分配給int的某個位置,以便以后可以將其用於平均。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM