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
Brick Number stored as text with openpyxl CAD79 2 362 Apr-17-2024, 10:17 AM
Last Post: CAD79
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 339 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 799 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 925 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 493 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  regex findall() returning weird result Radical 1 641 Oct-15-2023, 08:47 PM
Last Post: snippsat
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,053 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  capturing multiline output for number of parameters jss 3 809 Sep-01-2023, 05:42 PM
Last Post: jss
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 573 Aug-22-2023, 10:14 AM
Last Post: GYKR
  doubling a number Skaperen 8 1,213 Jul-25-2023, 10:20 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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