TypeError: must be real number, not str

47,003

Use int not str

from math import*

num1 = input("Enter the num ")
num2 = input("Enter the power ")

def exponent_func( num1 ,  num2):
     return(pow ( int(num1) , int(num2) ))  

exponent_func(num1 ,  num2)
Share:
47,003

Related videos on Youtube

Amy
Author by

Amy

Updated on July 31, 2022

Comments

  • Amy
    Amy over 1 year

    Whenever I rum the code, error message "TypeError: must be real number, not str" displays

    from math import*
    
    num1 = input("Enter the num ")
    num2 = input("Enter the power ")
    
    def exponent_func( num1 ,  num2):
         return(pow ( str(num1) , str(num2) ))  
    
    exponent_func(num1 ,  num2)
    
    • pault
      pault over 5 years
      You're casting the inputs to pow as str
  • pault
    pault over 5 years
    and what if someone wanted to get the square root of num1?
  • Amy
    Amy over 5 years
    Then we would use sqrt(x)
  • pault
    pault over 5 years
    @Amy what if someone wanted to get num1 raised to the power 0.1? I'm not being pedantic here, but it's important to understand why what you did does not work and what the limitations of this answer are. Edit: It's also worth reading Why is import * bad?
  • Amy
    Amy over 5 years
    Well this is my first day into Python programming and I don't have much experience in programming so this was just to get familiar with the syntax. I have updated my code to use float instead of int Thanks for the link!

Related