Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

Allow user to input a string variable and integer variable on the same line in Python.

I have a short assignment to write a python function that takes a user input string (one character) to represent a math function and a user input integer to represent a numerical value to process in that function.

The instructors notes show the prompt for input like this: "Enter a function and a value: "

And then the user input string and integer on the same line.

The user enters tangent of 30 like this: Enter a function and a value: T 30

I can write the function fine, that part is easy. I am having problems with how to parse the string and integer when they occur on the same line. I thought about: tuples, lists and I've also thought about just submitting it with the inputs on two different lines, but I have a feeling that will get me a significant point penalty.

Does anyone out there in Treehouse land know what code I should use to parse the single line and pass the two variables to my function?

Thanks to you, from me. Nancy M.

2 Answers

Hi Nancy M, If you're being asked to accept a single line input, you're dealing with a string. From the example in your question, it looks like the two values will be separated by a space which makes it nice to deal with. So it would be something like this:

    def nancysMathFunc(sillyStringInput):
        func, value = sillyStringInput.split(' ')
        value = int(value)
        if func == 'A':
            ***your fun formula to perform amazing calculations to value***
        elif func == 'D':
            ***your other equally impressive formula to perform calculations to value***

Hope that helps. By the way, if you can't be sure of the format of the input string, things get a little hairy but I'm sure we could figure it out. Don't take a point deduction and (more importantly) don't take the pass when it comes to figuring it out. GOOD LUCK!!

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

Thanks Grant and Chris. This should do the trick. Thank you for the Stack Overflow reference. I have yet to get the hang of using "split". I need to get with the program (so to speak.)

Have a great weekend whatever part of the globe you both inhabit.

NJM