Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error?
#1
The object of my program is to calculate one side of a triangle using pythagorean theorem. I feel like my code should work, but I keep receiving this error message:
Error:
Traceback (most recent call last): File "c:\users\julia\source\repos\Assignment12\Assignment12\Assignment12.py", line 19, in <module> ipt = int(input("Pythagorean theorem, Enter sides A and B and I will solve for C\n")) ValueError: invalid literal for int() with base 10: '3 4'
I am not quite sure where to go from here.

Thank you in advance for your assistance.

import math

def pythag( a, b, c1):

    if(c1 == 'c') :
        c = math.sqrt(pow(a, 2) + math.pow(b, 2))
    else :
        c = math.sqrt(pow(a,2) - math.pow(b,2))
    return c


ipt = int(input("Pythagorean theorem, Enter sides A and B and I will solve for C\n"))
a, b = ipt.split(" ")

print("Side C is equal to", pythag(a, b,'c'))

ipt = int(input("\nNow enter sides B and C and i will solve for A\n"))
a, b = ipt.split(' ')

print("Side A is equal to", pythag(b, a,'a'))

ipt = (input("\nFinally, enter sides A and C and i will solve for B\n"))
a, b = ipt.split(' ')

print("Side B is equal to", pythag(b, a,'b'))
Reply
#2
A way to solve this is to get rid of the ipt.split(" ") and change your int(input(...)) to eval(input(...)) like I do here,

import math
 
def pythag( a, b, c1):
 
    if(c1 == 'c') :
        c = math.sqrt(pow(a, 2) + math.pow(b, 2))
    else :
        c = math.sqrt(pow(a,2) - math.pow(b,2))
    return c
 
 
a,b = eval(input("Pythagorean theorem, Enter sides A and B and I will solve for C\n"))
print("Side C is equal to", pythag(a, b,'c'))
 
a,b = eval(input("\nNow enter sides B and C and i will solve for A\n")) 
print("Side A is equal to", pythag(b, a,'a'))
 
a,b = eval(input("\nFinally, enter sides A and C and i will solve for B\n"))
print("Side B is equal to", pythag(b, a,'b'))

Here's the output:
Output:
Pythagorean theorem, Enter sides A and B and I will solve for C 3,4 Side C is equal to 5.0 Now enter sides B and C and i will solve for A 4,5 Side A is equal to 3.0 Finally, enter sides A and C and i will solve for B 3,5 Side B is equal to 4.0

Also, while I'm still here, you should place your error in error tags, along with the entire error. Like this
Error:
Traceback (most recent call last): File "C:/Users/bagpi/Desktop/test.py", line 12, in <module> ipt = int(input("Pythagorean theorem, Enter sides A and B and I will solve for C\n")) ValueError: invalid literal for int() with base 10: '1 2'
Reply
#3
(Jul-02-2018, 03:51 AM)Zombie_Programming Wrote: A way to solve this is to get rid of the ipt.split(" ") and change your int(input(...)) to eval(input(...)) like I do here,

Using eval() on user input is a really bad thing. A user could enter code, for example, to delete all the files on your drive!

You are currently trying to apply int() directly to input() return string, but as you have a string with two 'numbers' in it with a space between, that will cause an error.

the ipt.split() method returns a list of strings. You have to convert each string to an integer.

Try this:

ipt = input("Pythagorean theorem, Enter sides A and B and I will solve for C\n")
a, b = [int(x) for x in ipt.split(" ")]
which uses list comprehension to step through each element in the list of strings from the split() method and covert it to an integer, leaving a list of integers in place of the list of strings. The assignment unpacks the two entries to a and b.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#4
(Jul-02-2018, 11:43 AM)gruntfutuk Wrote: Using eval() on user input is a really bad thing. A user could enter code, for example, to delete all the files on your drive!

I understand the consequences of using eval, I was simply trying to produce an example for the person to go off of. You could also get a and b in different input statements, like below.
import math
  
def pythag( a, b, c1):
  
    if(c1 == 'c') :
        c = math.sqrt(pow(a, 2) + math.pow(b, 2))
    else :
        c = math.sqrt(pow(a,2) - math.pow(b,2))
    return c
  
  
a = float(input("Pythagorean theorem, Enter side A"))
b = float(input("Enter side B and I will solve for C\n"))
print("Side C is equal to", pythag(a, b,'c'))
  
a = float(input("Pythagorean theorem, Enter side B"))
b = float(input("Enter side C and I will solve for A\n")) 
print("Side A is equal to", pythag(b, a,'a'))
  
a = float(input("Pythagorean theorem, Enter side A"))
b = float(input("Enter side C and I will solve for B\n"))
print("Side B is equal to", pythag(b, a,'b'))
Reply
#5
(Jul-03-2018, 02:49 AM)Zombie_Programming Wrote: I understand the consequences of using eval, I was simply trying to produce an example for the person to go off of.
you say to understand the consequences, yet advise/encourage a newbie asking in Homework section and clearly doesn't understand the risks to use it... Please, don't do so in the future. getting off is not an excuse in this case.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Jul-03-2018, 04:14 AM)buran Wrote: you say to understand the consequences, yet advise/encourage a newbie asking in Homework section and clearly doesn't understand the risks to use it... Please, don't do so in the future. getting off is not an excuse in this case.

In my case, I was taking experience from my CMPS 150 class in college. We were told to use eval() since int() and float() do not iterate. This shortened our code. However, I will be sure not to do so in the future. Hopefully I didn't upset you by doing it, honest mistake I promise <3
Reply
#7
(Jul-03-2018, 05:43 AM)Zombie_Programming Wrote: We were told to use eval() since int() and float() do not iterate.
I'm not sure what you mean by int() and float() do not iterate

(Jul-03-2018, 05:43 AM)Zombie_Programming Wrote: This shortened our code.
and also make it dangerous... no, thank you :-)

(Jul-03-2018, 05:43 AM)Zombie_Programming Wrote: Hopefully I didn't upset you by doing it
no, I'm not upset. But we try to avoid what is considered bad practices
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Jul-03-2018, 06:01 AM)buran Wrote: We were told to use eval() since int() and float() do not iterate.
I'm not sure what you mean by int() and float() do not iterate

I miss spoke by saying iterate, what I meant to say that having a user input numbers, like in the program the person was trying to do here, doing it with by saying something like a,b = int(input('Enter A and B: ")) will produce this error.

Error:
Traceback (most recent call last): File "C:\Users\bagpi\Desktop\test.py", line 1, in <module> a,b = int(input("Enter A and B: ")) ValueError: invalid literal for int() with base 10: '1,2'
Reply
#9
they should have teach you how to handle exceptions (validate user input), not to bypass it with eval :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(Jul-03-2018, 06:18 AM)Zombie_Programming Wrote:
(Jul-03-2018, 06:01 AM)buran Wrote: We were told to use eval() since int() and float() do not iterate.
I'm not sure what you mean by int() and float() do not iterate

I miss spoke by saying iterate, what I meant to say that having a user input numbers, like in the program the person was trying to do here, doing it with by saying something like a,b = int(input('Enter A and B: ")) will produce this error.

Error:
Traceback (most recent call last): File "C:\Users\bagpi\Desktop\test.py", line 1, in <module> a,b = int(input("Enter A and B: ")) ValueError: invalid literal for int() with base 10: '1,2'

The OP was trying to unpack two integer values from one string: not going to work.

I provided one alternative approach that is pretty succinct, although lacks error checking (so programme will stop if the user does not enter two integers with a space between them) that I thought sufficient for a beginner. I personally would normally write a function to manage data input and validation (probably using a try/exception block).

For a beginner, it might have been better to just have two input statements, frankly rather than collect two inputs at the same time.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

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