我正在为我正在采取的类(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 –