hi please help me to solve this problem. #Create a NEW TEXT FILE --> CEIT_News using the…
- User
hi please help me to solve this problem. #Create a NEW TEXT FILE --> CEIT_News using the open function.
------>> Remember that open function will be borrowed from a MODULE
#Write to the file the following message --> Fiji PM is visiting UPNG today. Now close file. #-------------------------------------------------------------------
#+++++++++++++++++++++++++++++++++++++Q4 (10 marks)+++++++++++++++++++++++++++++++++++++++++++++++++++++++ #Now create a function that will accept just one file name #Call this function --> READMYFILE #In this function, have the code that reads a file. #Use the return keyword in the function
#Now, invoke the function
- Assistant
Solution:
Create a new text file
file = open("CEIT_News.txt", "w") file.write("Fiji PM is visiting UPNG today. Now close file.") file.close()
Define a function to read a file
def READMYFILE(filename): file = open(filename, "r") content = file.read() file.close() return content
Invoke the function
file_content = READMYFILE("CEIT_News.txt") print(file_content) # Output: Fiji PM is visiting UPNG today. Now close file.
- User
i run the solution and it gives me error, please help me.# Create a new text file file = open("CEIT_News.txt", "w") file.write("Fiji PM is visiting UPNG today. Now close file.") file.close()
Define a function to read a file
def READMYFILE(filename): file = open(filename, "r") content = file.read() file.close() return content
Invoke the function
file_content = READMYFILE("CEIT_News.txt") print(file_content) # Output: Fiji PM is visiting UPNG today. Now close file.
- Assistant
It seems like the indentation is not correct in your code for the function definition. Also, there is a missing colon after the function name. Here's the updated code:
# Create a new text file file = open("CEIT_News.txt", "w") file.write("Fiji PM is visiting UPNG today. Now close file.") file.close() # Define a function to read a file def READMYFILE(filename): file = open(filename, "r") content = file.read() file.close() return content # Invoke the function file_content = READMYFILE("CEIT_News.txt") print(file_content) # Output: Fiji PM is visiting UPNG today. Now close file.Make sure that the indentation is correct when you run this code.
- User
- Assistant
The error message indicates that there is a problem with the indentation in your code. Python relies on indentation to determine the structure of the code, so it's important to make sure that your code is properly indented.
In this case, the
IndentationErroris caused by a missing or incorrect indentation on line 66, which is where thefilevariable is opened. Here's an example of how the code should be properly indented:# Define a function to read a file def read_file(filename): file = open(filename, "r") content = file.read() file.close() return content # Invoke the function on the "CEIT_News.txt" file file_content = read_file("CEIT_News.txt") print(file_content)In this updated code, the function definition is properly indented, and there should be no issues with the indentation. Make sure to check the indentation of your code and adjust it as necessary.
- User
please help me to answer this question. #See the program below '''Creaing a DB Cust_Order, creating a table CustInfo, inserting values into table and displaying the results''' #Identify the six (6) errors in the code and list them. #You can fix the code. (optional)
'''conn = MySQLdb.connect(host , user = "root", passwd = "ceit") mycursor = conn.cursor()
mycursor.execute('CREATE DATABASE Cust_Order') mycursor.execute('USE Cust_Order')
tableCust_order = """CREATE TABLE CustInfo(CustID int primary key, CustName , Address varchar)"""
insertbookvalue = """INSERT INTO CustInfo VALUES (101,"Jason Sam","Tokarara")""" mycursor.execute(insertbookvalue)
mycursor.execute() result = mycursor.fetchall() conn.commit()
for row in result: print(row) print("\n")'''
- Assistant
There are several errors in the code. Here's a list of the errors:
- The host parameter is missing in the
MySQLdb.connect()function call. - The
CustNameandAddresscolumns in theCustInfotable definition are missing their data types. - The
insertbookvaluestatement is missing a closing parenthesis for the values to be inserted. - The
mycursor.execute()statement on line 11 is missing the SQL statement to execute. - The
mycursor.fetchall()statement on line 12 is trying to fetch results before executing a query.
- The host parameter is missing in the
- User
thank you all the answers, really appreciated. please answer this question for me.#+++++++++++++++++++++++++++++++++++++++++Q1 (5 marks)+++++++++++++++++++++++++++++++++++++++++++++++++ #Write a program that takes in User's COURSE (CCDM, CCIT, CCNS or CCLSA) #And based on the course, it should print out the FEE #(If user inputs CCDM, it should output K3000) '''(If user inputs CCIT, it should output K3500) (If user inputs CCLSA, it should output K2500) (If user inputs CCNS, it should output K4200)'''
#+++++++++++++++++++++++++++++++++++++++Q2 i (3 marks)++++++++++++++++++++++++++++++++++++++++++++++ '''(i) Using the program you have in Question 1 Now, add one appropriate Exception handler to catch the error that may arise when User enters a numerical value.'''
- Assistant
Q1
Here's a program that takes in a user's course and prints out the corresponding fee:
course = input("Enter your course (CCDM, CCIT, CCNS or CCLSA): ") if course == "CCDM": print("Fee: K3000") elif course == "CCIT": print("Fee: K3500") elif course == "CCLSA": print("Fee: K2500") elif course == "CCNS": print("Fee: K4200") else: print("Invalid course")In this program, we use the
input()function to get the user's course. Then, we use theif-elif-elsestatement to check the course and print out the corresponding fee. If the user enters an invalid course, we print out an error message.Q2
To catch the error that may arise when the user enters a numerical value instead of a string, we can use a
try-exceptblock. Here's an updated version of the program with the exception handler added:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.