the Python problem 1. For variable product_code containing a string of letters a
ID: 3867306 • Letter: T
Question
the Python problem
1. For variable product_code containing a string of letters and digits,
(a) Give an if statement that outputs “Verified” if product_code contains both a “Z” and a “9”, and outputs “Failed” otherwise.
(b) Give a Python instruction that prints out just the last three characters in product_code.
2.
A. Given seconds from the user, write a function printedTime() that takes a Timer object and prints in the form of hours:minutes:seconds.
B. Write a function isAfter() which takes two Timer objects and returns True if the former time is later than the other time, and else returns false.
Explanation / Answer
A)
product_code="ABCZ0019" #set variable
index=0 # set these variable to 0
indexx=0
count=0
while index<len(product_code): #run while loop till end of string
if product_code[index]=='Z': # find the character
count=count+1 #increment the value of count
break;
index=index+1
while indexx<len(product_code):
if product_code[indexx]=="9":
count=count+1
break;
indexx=indexx+1
if count>1: #if the count value got incremented than it is verified
print "Verified"
else:
print "Failed"
B)
lastthreecharacter=product_code[-3:] #this syntax for printing last three characters
print lastthreecharacter
2)
def timeconvert(total): #declare the funtion accepting total sec as parameter
minutes=total/60 # find number of minutes the value can be greater than 60
seconds= total%60 # find seconds by mod operator
hours=minutes/60 # find hours by diving minutes by 60
minutes=minutes%60 # find exact minutes by mod operator
print ("%d:%d:%d" %(hours,minutes,seconds)) # print the values
timeconvert(908) #call the function
3)
def isAfter(t1,t2): #create a function with two arguements
if t1<t2: #string compare two string values
print t1 # print t1
else:
print t2
from threading import Timer #import the Timer class
t1=Timer #create two objects of it
t2=Timer
isAfter(t1,t2) # call the function with two object
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.