Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error?
#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


Messages In This Thread
Error? - by juliabrushett - Jul-02-2018, 03:07 AM
RE: Error? - by Zombie_Programming - Jul-02-2018, 03:51 AM
RE: Error? - by gruntfutuk - Jul-02-2018, 11:43 AM
RE: Error? - by Zombie_Programming - Jul-03-2018, 02:49 AM
RE: Error? - by buran - Jul-03-2018, 04:14 AM
RE: Error? - by Zombie_Programming - Jul-03-2018, 05:43 AM
RE: Error? - by buran - Jul-03-2018, 06:01 AM
RE: Error? - by Zombie_Programming - Jul-03-2018, 06:18 AM
RE: Error? - by gruntfutuk - Jul-03-2018, 11:22 AM
RE: Error? - by buran - Jul-03-2018, 06:23 AM

Forum Jump:

User Panel Messages

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