Write a function called mixNumberString that takes a number
(num)
If the number is divisible by 3, it should return a string with the
letter ‘z’ repeated P times where P is the remainder of input
parameter num divided by 3. I.e num%3.
If it is divisible by 5, it should return “Beep” repeated P times
where P is the remainder of input parameter num divided by 5. I.e
num%5.
If it is divisible by both 3 and 5, it should return 15.
Otherwise, it should return the same number.
Write a function for checking the speed of drivers. This function
should have one parameter: speed.
If speed is less than 70, it should return 0.
Otherwise, for every 5km above the speed limit (70), it should give
the driver one demerit point and print the total number of demerit
points. For example, if the speed is 80, it should return: 2
If the driver gets more than 12 points, the function should return
the speed.
Code:
def mixNumberString(num):
final_='' #empty string to return the final value
if (num%3==0 and num%5==0): #if num is divisible by both 3 and 5
then add 15
final_=final_+'15'
elif num%3==0: #if number is divisible by 3
for i in range(0,num//3): #iterate loop from 0 to num//3 eg:18 then
0 to 6
final_=final_+"z" #concatenate z to final_
elif num%5==0: #if num divisible by 5
for i in range(0,num//5): #concatenate Beep to final_
final_=final_+"Beep"
else:
final_=final_+str(num); #else add original number
return final_ #finallyy return the string
num=int(input("Enter the n value : "))
print(mixNumberString(num))
Code 1 and output Screenshots:
Code 2:
def CheckSpeed(speed): #check speed function
res=0 #final_result=0
if speed<70: #if speed less than 70 make res=0
res=0
elif (speed>=70): #if speed greater than 70
demerit_points=(speed-70)//5 #remove 70 from speed and then divide
by 5 because for every 5 kms above 70 speed
res=demerit_points #set demerit_points as result
if demerit_points>12: #if the demerit_points >12 set
res=speed
res=speed
return res #finally return the speed
speed=int(input("Enter the speed : ")) #asking user to enter the
speed
print(CheckSpeed(speed)) #calling CheckSpeed() function
Code 2 and Output Screenshots:
Note : in the question given p is nothing but num%3 but if we take like that num%3=0 so i have assumed it as num/3 times as p value.
Note : if you have any queries please post a comment thanks a lot..always available to help you
Get Answers For Free
Most questions answered within 1 hours.