Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Doubt in code
#1
Well i can simply put a variable in place of 'n' in square(n) but I tried to allow user to enter a number, but this code is not working and showing this error

Traceback (most recent call last):
File "python", line 10, in <module>
File "python", line 3, in square
TypeError: unsupported operand type(s) for ** or pow(): 'unicode' and 'int'

Code
def square(n):
    squared = n ** 2
    print "%d squared is %d." % (n, squared)
    return squared

n=raw_input("enter a no")
square(n)
Reply
#2
I'm not a expert, but I'm pretty sure its '%s' not '%d'.
Reply
#3
Use code tag BBcode help.
The problem is that raw_input return a string.
You should use Python 3 as a new user.
def square(n):
    squared = n ** 2
    print "{} squared is {}.".format(n, squared)
    # return square print or return choose one

# Make it return integer
n = int(raw_input("enter a no"))
square(n)
Output:
5 squared is 25.
Reply
#4
Here's another way in python3:

def square (n):
  print (n, "squared is ", int(n)**2)
  return

n = input("Enter a number: ")
square (n)
I may be wrong but raw_input was dropped in python3 to just input.
Reply
#5
thanks snippsat and Tim,for such helpful replies and that was a silly mistake at my end Tongue
Well im doing a basic pyhton course at "codecademy" right now and they use 2.7, but i'll definitely move to 3 as soon as the course end.
Reply
#6
I don't know why Codeacadamy (and others) don't wise up and update their sites, videos, blogs, etc to v3.  It's not like they haven't had ample opportunities to do so.  The Python gods have already decreed there will be no v2.8, so, as they say, why "beat a dead horse".  But that is a rant for another day.  If you must need to finish the course, by all means do, but then, as suggested, switch to the latest version of Python.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
(Aug-19-2017, 12:14 PM)sparkz_alot Wrote: I don't know why Codeacadamy (and others) don't wise up and update their sites, videos, blogs, etc to v3.  It's not like they haven't had ample opportunities to do so.

They have no incentive to expend the effort. Their customer base is people who want to learn Python, and therefore don't know about Python and the 2.x vs 3.x issues.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Doubt about conditionals in Python. Carmazum 6 1,605 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  A simple python doubt mohamedrabeek 2 727 Mar-26-2023, 07:24 PM
Last Post: deanhystad
  doubt about python tkinter and sqlite3 LONDER 2 2,153 Aug-14-2021, 08:48 AM
Last Post: ibreeden
  Python Doubt csrlima 5 2,602 Jan-23-2021, 12:23 AM
Last Post: csrlima
  List index out of range error when attempting to make a basic shift code djwilson0495 4 3,002 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Writing a basic shift code djwilson0495 2 2,268 Aug-16-2020, 01:52 PM
Last Post: djwilson0495
  Python Exercise Doubt azure 4 2,675 Apr-21-2020, 01:15 PM
Last Post: azure
  Doubt in Regex Lookaround fullstop 3 2,380 Feb-03-2020, 09:53 AM
Last Post: Gribouillis
  A doubt with 'in' and 'not in' operators with strings newbieAuggie2019 7 3,590 Oct-23-2019, 03:11 PM
Last Post: perfringo
  Problem with Basic Rock Paper Scissors code BirinderSingh 3 2,452 Sep-13-2019, 03:28 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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