要获得答案,请尽可能简单地避免使用任何过于先进的技术,因为我仍然在学习Python并且不想跳得太远......
对于def主程序,我试图在这个" Main"中运行所有3个函数,Summation和Multiply函数是自解释的,CreateIntList假设函数接受一个sentinel值(一个整数) ),和 它接受一个sentinal值(int)并返回一个整数列表。它会提示用户创建一个整数列表,使用提供的sentinel值作为用户输入的数字以退出列表的创建。
For each list:
o Tell the user which number list they are creating (starting at 1)
o Ask the user for their desired sentinel value
o Create a lists of integers (using the given sentinel value)
o Calculate the summation
o Calculate the product
o Print out:
The entire list
The summation of the list
The product of the list
def summation(intlist):
output = 0
for x in range(len(intlist)):
output += intlist[x]
return output
def multiply(intlist):
if len(intlist) == 0:
output = 0
return output
else:
output = 1
for x in range(len(intlist)):
output = intlist[x] * output
return output
def createIntList(myInt):
createlist = []
myInt = int(input("What do you want the sentinel to be?"))
addNum = None
while myInt != addNum:
addNum = input("Please enter a number, ", myInt, "to stop: ")
createlist.append(addNum)
return createlist
def main():
print("Welcome to the Simple Math Helper")
listIteration = int(input("How many list would you like to create? "))
x = 0
for x in range(len(listIteration)):
print("Your are creating list #", x)
createIntList(myInt)
multiply(createIntList)
summation(createIntList)
x = x + 1
print("Thank you for using the Simple Math Helper")
main()
预计输出:
Welcome to the Simple Math Helper
How many lists would you like to create? 1
You are creating list #1
What do you want the sentinel to be? 0
Please enter a number, 0 to stop: 1
Please enter a number, 0 to stop: 2
Please enter a number, 0 to stop: 3
Please enter a number, 0 to stop: 4
Please enter a number, 0 to stop: 5
Please enter a number, 0 to stop: 6
Please enter a number, 0 to stop: 7
Please enter a number, 0 to stop: 8
Please enter a number, 0 to stop: 9
Please enter a number, 0 to stop: 10
Please enter a number, 0 to stop: -9
Please enter a number, 0 to stop: -3
Please enter a number, 0 to stop: -1
Please enter a number, 0 to stop: 0
For the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -9, -3, -1]
The summation is 42
The product is -97977600
Thank you for using the Simple Math Helper
答案 0 :(得分:1)
Samira N已经给了您一些反馈,但您的代码还有许多其他问题。我评论了所有有错误的行并为您纠正了这些行。
def summation(intlist):
output = 0 #you are correct, as you are using it below, you need to initialize beforehand
#for x in range(len(intlist)): # see below for a better way to use list in a 'for'
for x in intlist: # 'x' now represents the proper element inside the list, iterating each element
#output += intlist[x] #see below on how to use 'x' with the new 'for'
output += x
return output
def multiply(intlist):
if len(intlist) == 0:
#output = 0 # no need for 'output'
#return output # same
return 0
else:
output = 1# same here, you are using output below, so you need to initialize beforehand. Nice!
#for x in range(len(intlist)): # same as before, see below for a better way to use 'for' with lists
for x in intlist:
#output = intlist[x] * output # same as before, use 'x' instead of 'intlist[x]' with the new 'for'
output = x * output
return output
#def createIntList(myInt): #you are initializing myInt in the input, so you dont need it here
def createIntList():
#createlist = [] # you should use better names for your variables
new_list = []
#myInt = int(input("What do you want the sentinel to be?")) # pick a better name for myInt
sentinel = int(input("What do you want the sentinel to be?"))
#addNum = None # same, better name
number = None
#while myInt != addNum: #see below for the 'while' with the new variable names
while sentinel != number:
#number = input("Please enter a number, ", sentinel, "to stop: ") # you need to use + to append, but you need to parse it to str as 'sentinel' is an int
number = int(input("Please enter a number, " + str(sentinel) + "to stop: ")) # same you did with 'sentinel', need to add 'int(' so you make sure it returns a number
#createlist.append(addNum) #same code below, but with the new variable names
if sentinel != number: # so you don't add the sentinel to the list
new_list.append(number)
return new_list
def main():
print("Welcome to the Simple Math Helper")
listIteration = int(input("How many list would you like to create? "))
#x = 0 # x is already being initialized/declared in the 'for' below
#for x in range(len(listIteration)): # listIteration is already an int, so you don't need to use len()
for x in range(listIteration):
print("Your are creating list #", x+1) #x+1 because x starts as 0, +1 ensures it will show 'Your are creating list #1' and so on
#createIntList(myInt) # you are calling the function with a variable that doesnt exist: myInt
int_list = createIntList() # as createIntList returns something, lets use int_list to save the returned list
#multiply(createIntList) # you need to send as paramter a variable (not the function itself)
multiplication_output = multiply(int_list) # you are returning an int, so you need to store it somewhere
#summation(createIntList) # send a variable, not the function
sum_output = summation(int_list) # same as before store the return somewhere
# x = x + 1 # no need for this, the 'for' does it already
print("For the list ", int_list)
print("The summation is ", sum_output)
print("The product is ", multiplication_output)
print("Thank you for using the Simple Math Helper")
main()
我试着按照你的要求保持简单:
要获得答案,请尽可能简单地避免使用任何过于先进的技术,因为我仍然在学习Python并且不想跳得太远......
但我会这样做:
def summation(int_list):
return sum(int_list) #sum() is a pretty good built-in function :)
def multiply(int_list):
if int_list: #better way to write 'if len(intlist) != 0:'
output = 1
for x in int_list:
output = x * output
return output
else:
return 0
def create_list(list_number):
print('Your are creating list #{}'.format(list_number))
new_list = []
sentinel = int(input('What do you want the sentinel to be? '))
number = None
while number != sentinel:
#str.format() is a function that I love.
#Check it out: https://pyformat.info/
number = int(
input('Please enter a number ({} to stop): '.format(sentinel)))
if number != sentinel:
new_list.append(number)
return new_list
def main():
print('Welcome to the Simple Math Helper')
listIteration = int(
input('How many list would you like to create? '))
for x in range(listIteration):
int_list = create_list(x+1)
multiplication_output = multiply(int_list)
sum_output = summation(int_list)
print('''For the list {}
The summation is {}
The product is {}'''.format(int_list, sum_output,multiplication_output))
print('Thank you for using the Simple Math Helper')
main()