x = input('Hey, wanna know the area of a triangle? Enter the height and base \\n')
for x_element in x.split(" "):
print(x_element)
So the goal is to create a function that calculates the area of a triangle. However when I use the code above and enter two values for the input statement, I'm thinking the split function will separate them into a list but instead, if I enter 5 6 it will create the following output
5
6
Both numbers are on new lines as opposed to making a list. I'm pretty new to python so I'm not sure why this is happening. Any help would be greatly appreciated.
Thanks,
CodePudding user response:
Yes, when x is the string '5 6', then x.split(' ') returns the list [5,6]. Your "for" loop is iterating over that list and printing each item in the list. By default, the print() function adds a newline after the arguments.
CodePudding user response:
You are printing each value in the list individually. By default, print
inserts a trailing newline, so they appear on separate lines.
If you want to print the list, just do so.
x = input('Hey, wanna know the area of a triangle? Enter the height and base \\n')
print(x.split(' '))