Python Forum
Prompting user for number, reading number, squaring it and returning result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prompting user for number, reading number, squaring it and returning result
#1
Hello.
What would be the code for a program that prompts the user for a number, reads that number, squares that number, and returns the resulting number?
Thank you!
Reply
#2
It's very simple to do this.
What have you done yet?
Reply
#3
Hi gontajones. Thank you for your reply.
Listen, I am new to Python. I have taken a couple of online courses that lasted a few hours each. Now I need to code.
Having said that, this is what I have done:

input("Enter a number")
user_answer = input("Enter a number")
user_answer_squared = user_answer**2
print(user_answer_squared)
Reply
#4
That's a start!
Check this and tell me if you understand it.

user_answer = input("Enter a number: ")
user_answer_squared = int(user_answer)**2
print(user_answer_squared)
PS: Use the python code tag when post code.
Reply
#5
Awesome, thank you very much! Greatly appreciated.
Reply
#6
(Sep-12-2018, 11:19 PM)JHPythonLearner Wrote: Hi gontajones. Thank you for your reply.
Listen, I am new to Python. I have taken a couple of online courses that lasted a few hours each. Now I need to code.
Having said that, this is what I have done:

input("Enter a number")
user_answer = input("Enter a number")
user_answer_squared = user_answer**2
print(user_answer_squared)
In Python 3.x (which I hope you are using) ALL input is a string. That is, a series of unicode characters that Python does not treat as a numeric value.

You need to tell Python to cast a string to the kind of number you want. IF the string is not valid as a number, an exception will be raised and your programme will halt with an error message.

num = int(user_answer)
will attempt to convert the string referenced by user_answer into an integer referenced by num which you can then use in your subsequent code.

You can use float() to cast to a floating point number.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing the code line number arbiel 2 74 45 minutes ago
Last Post: arbiel
  Finding the price based on industry and number of transactions chandramouliarun 1 1,071 Jun-04-2024, 06:57 PM
Last Post: marythodge4
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 267 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  intering int number akbarza 1 367 Apr-28-2024, 08:55 AM
Last Post: Gribouillis
Brick Number stored as text with openpyxl CAD79 2 741 Apr-17-2024, 10:17 AM
Last Post: CAD79
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 487 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 967 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 1,182 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 588 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  regex findall() returning weird result Radical 1 755 Oct-15-2023, 08:47 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020