Square a number with functions in python [duplicate]

2024/11/18 23:40:41

This is an extremely easy question for Python. It's very basic Python as I'm still a beginner... to take a number, use a function and square it:

import math
nmb = int(raw_input("Enter a number to be squared: "))
def square(nmb):y = math.pow(nmb,2)return y
print str(nmb) + " squared is equal to " + str(square)

I've jiggered it around a few times, but the end result always prints something like "5 squared is equal to function square at 0x02BC87B0" instead of the result

I feel like I'm missing something really obvious, as my understanding of functions is still quite basic, but any pointers would set me on my way!

Answer

You are passing the function square, not the return value of a call to square, to str. You want this:

print str(nmb) + " squared is equal to " + str(square(nmb))
https://en.xdnf.cn/q/118612.html

Related Q&A

Changing the cell name

I have a file that contains the following:NameABCD0145ABCD1445ABCD0998And Im trying to write a cod that read every row and change the name to the following format:NameABCD_145ABCD_1445ABCD_998keeping i…

Procfile Heroku

I tried to deploy my first Telegram chatbot (done with Chatterbot library) on Heroku. The files of my chatbot are: requirements (txt file) Procfile (worker: python magghybot.py) botusers (csv file) Mag…

How do i loop a code until a certain number is created?

This task is to determine the difference between two attributes, strength and skill, from game characters. The process for this is:Determining the difference between the strength attributes. The differ…

Finding the longest list in given list that contains only positive numbers in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed l…

How to create multiple VideoCapture Objects

I wanted to create multiple VideoCapture Objects for stitching video from multiple cameras to a single video mashup.for example: I have path for three videos that I wanted to be read using Video Captur…

How to read Data from Url in python using Pandas?

I am trying to read the text data from the Url mentioned in the code. But it throws an error:ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2url="https://cdn.upgrad.…

Testing multiple string in conditions in list comprehension [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 6 years ago.I am trying to add multiple or clauses to a python if statement…

Filter range from two dates in the same query Django/Python

I need the result from a query that filters two dates from the same model. I need to get in the result 5 days (today plus 4 days) from original date and sale from target date (today plus 4 more days) b…

Python While/For loop

how can I make this into a while loop and output the same thing????for x in range(56,120) :if (x < 57) :summation = 0summation = x + summationif (x == 119) :print (“Sum of integers from 56 to 1…

Read a file into a nested dictionary?

Say I have a simple file like so holding arbitrary values:A, 20, Monday, 14, Tuesday, 15, Tuesday, 16 B, 40, Wednesday, 14, Friday, 12How would I get it into a nested dictionary so that each k/v pair l…