Kayla Hiltermann
2015-05-09 03:24:48 UTC
hi,
i am trying to make a pythagorean triples checker (a^2 + b^2 = c^2). the user enters three sides to a triangle and my code determines if it is a pythagorean triple (aka right triangle) or not. i have the entire code pretty much done, except i want to account for variability in user input, like using commas or just spaces. the user input is initially a string, but is converted to a list once run through .split() . I would like to split the user input by commas or spaces, so:
3 4 5
3,4,5
3, 4, 5
all become: [“3", “4", “5"]. yes, the inputs are strings but i convert - or make sure - they are integers later in the program. my main issue is that i cannot split by commas or spaces at the same time. i tried using the vertical bar - .split(“ |,”) but it only executed the split by space, not the split by comma. as of now, i can only split by either. the issue is that when i split by spaces - .split(“ ) , “3,4,5” does not split and becomes the list [“3,4,5”]. on the other hand, “3, 4, 5” does split, but becomes [“3,”, “4,”, “5”]. the problem is the same if i replace the .split(“ “) by split(“,”), only the commas are replaced by spaces.
sorry if that was super confusing. below is my code. there is a commented out section after sides = raw_input … that i left in for reference. all other parts of the code work. any suggestions would be greatly appreciated!! thanks
import re
def pythagorean_function():
sides = raw_input("Please enter three sides to a triangle: \n").split(" |,")
'''
when sides is created, it is a string. when it is split by a .split action,
it becomes a list regardless if the items in sides are actually split or not.
i can't figure out how to split sides (when created) by a comma OR a space.
the following code attempts (and fails).
re.split(r",| ",sides)
print sides
for item in sides:
if "," in item:
re.split(r",",item)
print "comma"
'''
sides_int = []
for value in sides:
try:
sides_int.append(int(value))
except ValueError:
continue
while len(sides_int) != 3:
print ("you did not enter THREE sides! remember all sides must be integers \n")
break
sides.sort()
if sides[0]**2 + sides[1]**2 == sides[2]**2:
print "\nthis triangle IS a pythagorean triple!\n"
else:
print "\nthis triangle is NOT a pythagorean triple\n"
redo()
def redo():
redo_question = raw_input("would you like to see if another triangle is a pythagorean triple? Y/N\n")
if redo_question == "Y":
pythagorean_function()
else:
print "thanks for stopping by!"
pythagorean_function()
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/t
i am trying to make a pythagorean triples checker (a^2 + b^2 = c^2). the user enters three sides to a triangle and my code determines if it is a pythagorean triple (aka right triangle) or not. i have the entire code pretty much done, except i want to account for variability in user input, like using commas or just spaces. the user input is initially a string, but is converted to a list once run through .split() . I would like to split the user input by commas or spaces, so:
3 4 5
3,4,5
3, 4, 5
all become: [“3", “4", “5"]. yes, the inputs are strings but i convert - or make sure - they are integers later in the program. my main issue is that i cannot split by commas or spaces at the same time. i tried using the vertical bar - .split(“ |,”) but it only executed the split by space, not the split by comma. as of now, i can only split by either. the issue is that when i split by spaces - .split(“ ) , “3,4,5” does not split and becomes the list [“3,4,5”]. on the other hand, “3, 4, 5” does split, but becomes [“3,”, “4,”, “5”]. the problem is the same if i replace the .split(“ “) by split(“,”), only the commas are replaced by spaces.
sorry if that was super confusing. below is my code. there is a commented out section after sides = raw_input … that i left in for reference. all other parts of the code work. any suggestions would be greatly appreciated!! thanks
import re
def pythagorean_function():
sides = raw_input("Please enter three sides to a triangle: \n").split(" |,")
'''
when sides is created, it is a string. when it is split by a .split action,
it becomes a list regardless if the items in sides are actually split or not.
i can't figure out how to split sides (when created) by a comma OR a space.
the following code attempts (and fails).
re.split(r",| ",sides)
print sides
for item in sides:
if "," in item:
re.split(r",",item)
print "comma"
'''
sides_int = []
for value in sides:
try:
sides_int.append(int(value))
except ValueError:
continue
while len(sides_int) != 3:
print ("you did not enter THREE sides! remember all sides must be integers \n")
break
sides.sort()
if sides[0]**2 + sides[1]**2 == sides[2]**2:
print "\nthis triangle IS a pythagorean triple!\n"
else:
print "\nthis triangle is NOT a pythagorean triple\n"
redo()
def redo():
redo_question = raw_input("would you like to see if another triangle is a pythagorean triple? Y/N\n")
if redo_question == "Y":
pythagorean_function()
else:
print "thanks for stopping by!"
pythagorean_function()
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/t