Question

In: Computer Science

What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at...

What does each line of this python code do?

import datetime
import getpass
print("\n\nFinished execution at ", datetime.datetime.now())
print(getpass.getuser())

Solutions

Expert Solution

In Python date and time are not inbuilt datatypes on their own but a module named datetime can be imported to work with the date as well as time.

1)import datetime----->>>Here we are importing a package to our python which consists of date and time.Its a combination of date and time along with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.

2)import getpass---->>>It is a portable password input.By default the keys entered by user in the terminal are not echoed. Also the default prompt that appears on terminal is ‘password’ which can be customized by providing a string as parameter.

3)print("\n\nFinished execution at ", datetime.datetime.now())------>>>It helps us get the current date and time,since we are using inside the print.Hence the date and time gets printed.

4)print(getpass.getuser())------->>>As I previously mentioned that the getpass() function prints a prompt then reads input from the user until they press return and with the combination of getpass and getuser,Sometimes we need to know the login name which we are using to run the script. This is achieved by using the getuser() function.

Example::

import getpass
user = getpass.getuser()
while True:
   psd = getpass.getpass("User Name : ",user)
   if psd == 'Jack':
      print("You are loggedin")
   else:
      print("The password entered by you is wrong.")

output::

User Name: user1
You are loggedin !

Related Solutions

fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
. What does each line of the following code do? fsrPin = ‘A4’; ledPin = ‘D4’;...
. What does each line of the following code do? fsrPin = ‘A4’; ledPin = ‘D4’; anArduinoObj = arduino(); forceVoltage = readVoltage(anArduinoObj, fsrPin); if forceVoltage > 4.0 writeDigitalPin(anArduinoObj, ledPin, 1); end Based on the Adafruit pages related to their round FSR sensor, what is the required force to read a voltage greater than 4V?
Python 3 Calendar does not showing up Fix code: # required library import tkinter as tk...
Python 3 Calendar does not showing up Fix code: # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) # arraging barcode.grid(row=0, column=1) product.grid(row=1, column=1) money.grid(row=2, column=1) cal = DateEntry(window, width=12, year=2019,...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
explain the code for a beginner in c what each line do Question 3. In the...
explain the code for a beginner in c what each line do Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable...
Python 3 can someone explain in very simple terms what EVERY line of code does, including...
Python 3 can someone explain in very simple terms what EVERY line of code does, including what j and i do def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('The average is',avg/(end-start)) s=input('Enter a string ') longest(s)
Python 3 can you explain what every single line of code does in simple terms please...
Python 3 can you explain what every single line of code does in simple terms please so basically # every single line with an explanation thanks def longest(string): start = 0;end = 1;i = 0; while i<len(string): j = i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: end = j start = i i = j; avg = 0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('The average is',avg/(end-start)) s = input('Enter a string ')...
Run the  Python Queue Line Simulator  three times Python Queue Line """ File: pyQueueSim.py Author: JD """ import...
Run the  Python Queue Line Simulator  three times Python Queue Line """ File: pyQueueSim.py Author: JD """ import random print("Queue as a customer line\n") queue = []              # Empty que y = int(0) # Queue up some customers for i in range(1,20):     x = random.randint(1, 20)     if x >= 2 and x<= 8:       queue.append(x)      # Add to the front        # Simulate cumstomer line processing while True:    x = random.randint(1, 20)    if x >= 2 and x<= 8:       queue.append(x *2)      # Add to the front...
Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT