Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with my program
#1
This is supposed to be the outcome of my code
Output:
Enter your choice: 3 Enter the value of the base followed by the exponent: 5 3 The power of 5 raised to 3 is 125.
my code is
n,exp=int(input("Enter the value of the base:")).split(",")
      def power(n,exp):
         if(exp==1):
            return(n)
         if(exp!=1):
           return(n*power(n,exp-1))
print("The power of",n, "raised to",exp,"is",power(n,exp))
This is the error i receive
Error:
Traceback (most recent call last): File "C:\Users\NICKLAUS\Desktop\LE3222_RosalesNicklaus.py", line 1, in <module> n,exp=int(input("Enter the value of the base:")).split(",") ValueError: invalid literal for int() with base 10: '3,5'
Reply
#2
If you have such an error in a compound statement, you must split it to see where exactly the error arises. The message tells you the error is in:
n,exp=int(input("Enter the value of the base:")).split(",")
The first part to be executed will be:
input("Enter the value of the base:")
When you enter the string: '3,5', this will be replaced with '3,5' in the next stage:
>>> int('3,5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3,5'
This gives you the answer: '3,5' is not an integer. int() expects one parameter, so the most readable construction would be to split the line into:
n,exp=input("Enter the value of the base:").split(",")
n = int(n)
exp = int(exp)
The most
Reply


Forum Jump:

User Panel Messages

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