Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

URGENT I need Python 3 Help. I need to write a program that uses functions to ask user input on a file name to open, anyone able to help me? I tried, my code just doesnt work, i get an error saying the file name is not defined. Heres what i had gotten: ``` def main(): filename = GetInputFileName(input('Enter file name: ')) inFile = open(filename,'r') return filename ```

OpenStudy (anonymous):

This isnt all my program has to do, its just the first part and already having issues.

OpenStudy (anonymous):

Hi. I think there's a problem with using `input()`. It seems that it is trying to evaluate the expression which in this case is the file name and which can't exactly be evaluate. I used `raw_input()` and the program works for me. You have my code below: ``` def main(): filename = get_input_filename() in_file = open(filename,'r') # example of doing something with the file content for line in in_file.readlines(): print(line) return filename def get_input_filename(): filename = raw_input("Enter file name: ") return filename print(main()) ``` From what I have read in python 3.x versions this shouldn't be a problem since the old `raw_input()` has been renamed to `input()`. This has been done because when someone inputs something, they are more likely to want the actual string than an evaluation for it. The old functionality of the `input()` can be obtained with `eval(input())`. Hope this helps.

OpenStudy (anonymous):

aweome it works but i changed raw_input to input

OpenStudy (anonymous):

ill try and get the next part of the assignment myself but ill ask if i get stuck

OpenStudy (e.mccormick):

2.x raw_input = 3.x input.

OpenStudy (anonymous):

ahh alright thanks mccorm

OpenStudy (anonymous):

to make it use a different function 'print_report_header()' to print out a table header the headers are: region, basic subscriptions, educational supplement, annual update

OpenStudy (anonymous):

would i do

OpenStudy (anonymous):

do i do it in a print statement? not really sure

OpenStudy (anonymous):

and just format it

OpenStudy (e.mccormick):

Well, obviously it all goes in a function block... but yah, you would just use the assorted formatting tools to get things to line up the way you want when printed.

OpenStudy (anonymous):

alright thank you, ill give that a shot

OpenStudy (anonymous):

``` def get_input_filename(): filename = input("Enter your input file name: ") return filename def print_report_header(): def main(): filename = get_input_filename() in_file = open(filename,'r') # example of doing something with the file content for line in in_file.readlines(): return filename print('--------------------------------------------------') print('Company XYZ') print('Monthly Sales Report') print('--------------------------------------------------') main() ``` The part with the ``` print('--------------------------------------------------') print('Company XYZ') print('Monthly Sales Report') print('--------------------------------------------------') ``` how do i make that print? its not printing it for some reason.

OpenStudy (anonymous):

@e.mccormick

OpenStudy (e.mccormick):

You did a return before you got there.

OpenStudy (anonymous):

ooh ok yeah its working now, didnt know that was something to watch out for thanks

OpenStudy (anonymous):

now if i put the headers inside the print_report_header() function how do i make it print later? i have to make it actually print from the main function right?

OpenStudy (e.mccormick):

Basically, you have it call the function when needed. Wherever the report is made, that header is called first.

OpenStudy (anonymous):

Hm got it. Ok down to the last part, how do I actually use the data in the file. One set of My data: LA 150 120 105 How do I actually use that? Like I need to put it in a chart by using a function.

OpenStudy (e.mccormick):

Dependoing on how the data is stored, you will parse the file by reading in one or more lines. You can read in data one chunk at a time and then print it, or you can read in everything into an array of some sort and then print the array. Both ways work.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!