Question
Using python
1. Write: A program that implements a logbook, recording the visitors to a place of interest and their visits purpose. This log book could later be read by another program. Implement a function called log that takes one parameter, filename, the name of a file. You may assume the file is in the current working directory of the program. log should prompt the user for their name arid the nature of their visit. log should then append a line to the file named filename in the following format: 1 KNAME OF INDIVIDUAL> <NATURE OF VISIT> Additional requirements e If the user enters empty input, or input that is only whitespace, re-prompt them for valid input before continuing. Your program must not remove previous lines from the file. For example, assume the following is the current contents of a file log.txt: 1 Winston Churchill 1 The Queen is expecting me Running logllog.txt) results in the following exchange: What is your name?: James Bond hat is the nature of your visit?: To save the Queen The file log.txt should now contain: : Winston Churchill I The Queen is expecting me lames Bond | To save the Queen
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#!/usr/bin/env python
def insertRecord(name, purpose):
with open('log.txt', 'a') as logFile:
    textToWrite= name + "|" + purpose + '\n' # generate string to append in log file use '\n' for line terminator
    logFile.write(textToWrite) # append string to log file

def getValidInput(str):
while True:
    value = raw_input(str) # get input
    if value.strip() == "": # if input is only white space
      print "Please enter a valid input" # Ask user to provide valid input
    else:
      return value

def main():

     name = getValidInput("Enter your name:")# get visitor name
     purpose = getValidInput("Enter purpose of visit:")#get purpose of visit
     insertRecord(name, purpose)# Update into Log
if __name__ == '__main__':
    main()


186590d3efc9:python kumsanjis 186590d3efc9:python kumsanj1$ python logRecord.py Enter your name:John Cena Enter purpose of visit:Kill Undertaker 186590d3efc9:python kumsanjis 186590d3efc9:python kumsanji$ cat log.txt John Cena |Kill Undertaker 186590d3efc9:python kumsanjis 186590d3efc9:python kumsanj1$ python logRecord.py Enter your name: Please enter a valid input Enter your name: Sanjiv Kumar Enter purpose of visit:Write a Program 186590d3efc9:python kumsanjis 186590d3efc9:python kumsanjis 186590d3efc9:python kumsanji$ cat log.txt John Cena |Kill Undertaker Sanjiv Kumar |Write a Program 186590d3efc9:python kumsanji!$

Add a comment
Know the answer?
Add Answer to:
Using python 1. Write: A program that implements a logbook, recording the visitors to a place...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Write a function file_copy() that takes two string parameters: in file and out_file and copies...

    2. Write a function file_copy() that takes two string parameters: in file and out_file and copies the contents of in_file into out file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output. 1 >>> file_copy( created equal.txt', 'copy.txt) 2 >>> open fopen ( copy.txt') 3 >>> equal-f = open( 'created-equal . txt') 4 >>equal_f.read () 5 'We hold these truths to be self-evident, Inthat all men are created equalIn' 3. Write:...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • (Count the occurrences of each keyword) Write a python program that reads in a Python source...

    (Count the occurrences of each keyword) Write a python program that reads in a Python source code file and counts the occurrence of each keyword in the file. Your program should prompt the user to enter the Python source code filename.

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...

    Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns,...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT