简体   繁体   English

编写一个函数对一个数进行平方,然后用它来编写一个函数,该函数接受三个整数并返回它们的平方和

[英]Write a function which squares a number and then use it to write a function which takes three integers and returns the sum of their squares

Im new to python and cant figure out how to get these functions to call themselves.我是 python 新手,无法弄清楚如何让这些函数调用自己。 It asks for an input but no matter what gives 0 as the output.它要求输入,但无论如何都给出 0 作为输出。 Can someone help debug?有人可以帮忙调试吗?

userinput = input("Enter three numbers: ")
userinput = userinput.split(',')
finalsum = 0
finaldata = []

def formatinput(x):
 sqrdata = []
  for element in x:    
    sqrdata.append(int(element))
  return(sqrdata)

def findsquare(x):
 return (x*x) 

def sumthesquares(y):
  for element in y:
    temp = findsquare(element)
    finaldata.append(int(temp))
    finalsum = finalsum + temp
  return finalsum

def findthesquares(userinput):
  finalsum = sumthesquares(formatinput(userinput))

print(finalsum)

Have you actually tried running your code?你真的试过运行你的代码吗? From what you've posted, it looks like you never actually call your functions...从您发布的内容来看,您似乎从未真正调用过您的函数...

They're defined, but you're missing the actual calls, like formatinput(userinput) .它们已定义,但您错过了实际调用,例如formatinput(userinput)

For future reference, if you put something like print("Got here!") into your functions, you can test that they're being called.为了将来参考,如果您将print("Got here!")放入您的函数中,您可以测试它们是否被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 取正整数n并返回小于n的所有正整数的平方和的Python函数 - Python function that takes a positive integer n and returns the sum of the squares of all the positive integers smaller than n 将最小二乘解返回到线性矩阵方程的函数 - Function which returns the least-squares solution to a linear matrix equation 编写一个函数来验证它作为参数的数字是否是阿姆斯特朗数 - Write a function which verifies if the number it takes as parameter is an Armstrong number or not 返回平方和函数 - Returning a sum of squares function 平方和函数 - Sum of squares function 编写一个函数 sum_integers(start, end) ,它接受两个正整数 start 和 end ,并返回所有整数的总和 - Write a function sum_integers(start, end) that takes two positive integers, start and end , and returns the sum of all integers 编写一个 evenNumOfOdds (L) 函数,它接受一个正整数列表 L,如果 L 中有偶数个奇数,则返回 True - write a function of evenNumOfOdds (L) that takes in a list of positive integers L and returns True if there are an even number of odd integers in L 从 1 到 x 的偶数平方和使用 python function - Sum of squares of even integers from 1 to x using python function 数字是 Square 或可以写成两个平方之和 python - number which is Square or can be written as sum of two squares python 如何编写一个切片的函数? - How to write a function which takes a slice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM