我的目标是:
我试图捕获main中变量close的布尔值,但出现错误。
错误:“关闭= addToList()类型错误:addToList()缺少1个必需项 位置参数:“ numberList”。”
#function to create a list
def createList():
numberList = []
return numberList
#function to add integers from user to list
def addToList(numberList):
stopAdding = False
close = False
while stopAdding == False:
integerInput = input("Please enter a number [1-9] or '0' to stop: ")
if integerInput.isalpha():
badInput()
close = True
break
elif integerInput == '0':
stopAdding = True
else:
numberList.append(int(integerInput))
if close == True:
return close
else:
return numberList
#function for when invalid number entered
def badInput():
print("Invalid number entered.")
#function to sum the numbers in list
def sumList(numberList):
sumTotal = 0
for number in numberList:
sumTotal = sumTotal + number
return sumTotal
#function to print the list sum
def printSum(sumTotal):
print("The sum is: ", sumTotal)
#the main function
def main():
numberList = createList()
addToList(numberList)
sumTotal = sumList(numberList)
close = addToList()
if close == True:
pass
else:
printSum(sumTotal)
#call main
main()
如果输入了非整数,有什么更好的方式正常退出?谢谢。
答案 0 :(得分:1)
将您的backup_master
函数更改为以下形式;
main
要使代码更整洁,您还可以执行其他操作,即删除def main():
numberList = createList()
close = addToList(numberList)
sumTotal = sumList(numberList)
if not close:
printSum(sumTotal)
函数并仅使用内置的sumList
函数,然后删除sum
,因为它实际上并不需要成为它自己的功能。然后您的createList
会看起来像这样;
main
答案 1 :(得分:0)
您在第二次通话中忘记了论点:
close = addToList(sumTotal)
答案 2 :(得分:-1)
谢谢伙计们。
我在addToList函数末尾摆脱了if if else,并返回了close值和numberList值:return(close,numberList) 然后主要用于内置sum函数:
var utilsModule = require("tns-core-modules/utils/utils");
public CallLog= android.provider.CallLog;
public Log = android.util.Log;
public Uri = android.net.Uri;
var callUri = this.Uri.parse("content://call_log/calls");
var strOrder = android.provider.CallLog.Calls.DATE + " DESC";
var context = utilsModule.ad.getApplicationContext();
var cr=context.getContentResolver();
var curCallLogs = cr.query(callUri, null, null, null, strOrder);
//I just need latest 30 calls
for(var i=0;i<30;i++){
curCallLogs.moveToNext();
var strName=curCallLogs.getString(curCallLogs.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
callobject.callerNumber=curCallLogs.getString(curCallLogs.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
}
最后,为了排除所有输入的非整数输入,我使用了:
close, numberList = addToList(numberList)
#the main function
def main():
numberList = createList()
close, numberList = addToList(numberList)
if not close:
printSum(sum(numberList))
现在运行良好。