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 ```
This isnt all my program has to do, its just the first part and already having issues.
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.
aweome it works but i changed raw_input to input
ill try and get the next part of the assignment myself but ill ask if i get stuck
2.x raw_input = 3.x input.
ahh alright thanks mccorm
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
would i do
do i do it in a print statement? not really sure
and just format it
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.
alright thank you, ill give that a shot
``` 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.
@e.mccormick
You did a return before you got there.
ooh ok yeah its working now, didnt know that was something to watch out for thanks
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?
Basically, you have it call the function when needed. Wherever the report is made, that header is called first.
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.
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.
Join our real-time social learning platform and learn together with your friends!