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
#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


Messages In This Thread
RE: Prompting user for number, reading number, squaring it and returning result - by gruntfutuk - Sep-13-2018, 04:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 147 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  intering int number akbarza 1 275 Apr-28-2024, 08:55 AM
Last Post: Gribouillis
Brick Number stored as text with openpyxl CAD79 2 576 Apr-17-2024, 10:17 AM
Last Post: CAD79
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 424 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 898 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 1,055 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 542 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  regex findall() returning weird result Radical 1 709 Oct-15-2023, 08:47 PM
Last Post: snippsat
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,137 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  capturing multiline output for number of parameters jss 3 866 Sep-01-2023, 05:42 PM
Last Post: jss

Forum Jump:

User Panel Messages

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