我正在爲我的介紹級別CS類工作,我不確定如何解決此任務。這裏是:命令行輸入,極端和地圖模式python
創建一個程序,其中需要命令行輸入來獲取數據和 使用列表處理數據。下面是該程序應如何響應:
[email protected]:~/cs150/loops$ python3 command.py 11 5 3 51 the smallest number is 3
要將參數轉換爲整數,使用地圖的圖案,如:在List.py
strings = ArrayToList(sys.argv[1:]) numbers = ListMap(int,strings)
該列表地圖功能,爲你定義了,你
wget troll.cs.ua.edu/cs150/book/List.py
您需要導入您的程序列表和sys模塊: 可以下載
import sys from List import *
最後,使用列表中的極端模式來查找和打印 最小整數一旦列表地圖已轉換的參數字符串轉換爲整數 。
以下是我有:
import sys
from List import *
def main():
strings = ArrayToList(sys.argv[1:])
numbers = ListMap(int,strings)
smallest = numbers[0]
for i in range(0,len(numbers),1):
if (numbers[i] < smallest):
smallest = numbers[i]
print("The smallest number is: ", smallest)
main()
下面是我從這個代碼收到錯誤消息:
File "command.py", line 12, in <module>
main()
File "command.py", line 9, in main
if (numbers[i] < smallest):
TypeError: unorderable types: list() < int()
很抱歉,這是這麼長的帖子,我真的很需要一些幫助因爲我是編碼新手,我想徹底。不知道我出錯的地方或我需要添加/更改的地方,任何幫助將不勝感激。提前致謝。
'ListMap'包含'int'和'list'元素嗎?在if語句之前,嘗試在你的循環中打印每個'numbers [i]'。同時打印'type(numbers [i])',以便知道它們的類型。用打印的內容更新你的問題會有所幫助。 –
Python中不需要'range(0,len(numbers))'。只是'數字編號'。吉茲,你甚至不必要地指定了1步! :P –