我正在爲我正在採取的類(Python 3)我的最後一部分在這部分卡住了。使一個函數,採取一個整數,並將其減少到奇數
他給了我們一個裏面有數字的文件。我們打開它並將這些數字添加到列表中。
「創建一個名爲makeOdd()的函數,返回一個整數值,該函數應該取任意整數,並將其減半,直到它變爲奇數爲止。 10將減少一半到5 o 9已經是奇數,所以它會保持爲9. o但是12會減半到6,然後再減半到3. o雖然16會被切到8被削減到4被削減到2被削減到1. 將這個函數應用到數組中的每一個數字「
我試圖搜索互聯網,但我還沒有線索在哪裏甚至從這一開始。你能幫忙的話,我會很高興。
這裏我的整個決賽至今:
#imports needed to run this code.
from Final_Functions import *
#Defines empty list
myList = []
sumthing = 0
sortList = []
oddList = []
count = 0
#Starts the Final Project with my name,class, and quarter
intro()
print("***************************************************************",'\n')
#Opens the data file and reads it then places the intrager into a list we can use later.
with open('FinalData.Data', 'r') as f:
myList = [line.strip() for line in f]
print("File Read Complete",'\n')
#Finds the Sum and Adverage of this list from FinalData.Data
print("*******************sum and avg*********************************")
for oneLine in myList:
tempNum = int(oneLine)
sumthing = sumthing + tempNum
avg = sumthing /1111
print("The Sum of the List is:",sumthing)
print("The Adverage of the List is:",avg,'\n')
print("***************************************************************",'\n')
#finds and prints off the first Ten and the last ten numbers in the list
firstTen(myList)
lastTen(myList)
print("***************************************************************",'\n')
#Lest sort the list then find the first and last ten numbers in this list
sortList = myList
sortList.sort()
firstTen(sortList)
lastTen(sortList)
print("****************************************************************",'\n')
語言:Python 3的
你到目前爲止嘗試過什麼嗎?請向我們展示您的努力。 –
「我不知道哪裏可以開始」 - 真的嗎?不知道如何打開一個文件,不知道如何讀取數字,不知道如何劃分兩個,不知道如何檢查一個數字是奇數還是偶數,都不是這樣? – zvone
我已經做了前半部分的前軀只是我發佈的部分我不知道該怎麼做。我只列出了所有的偶數。對於i在myList中: 如果計數%2 == 1: oddList.append(ⅰ) 計數+ = 1 –