In: Computer Science
Use Python Return a password string constructed with the
following rules:
(1) If there are only 0 or 1 numeric among 3 parameters,
concatenate
the string form of all parameters directly.
(2) If there are 2 numeric parameters, calculate the sum of 2
numeric
values if they are both int, or calculate the product of them and
round
to 2 decimal places if any of them is float. Finally, append
this
sum/product to the string parameter.
You may assume that there will be at least 1 string parameter.
eg.>>> password_2("Marina", "Langlois", "20")
'MarinaLanglois20'
>>> password_2("Marina", "Langlois", 20)
'MarinaLanglois20'
>>> password_2("Elvy", True, 20)
'ElvyTrue20'
>>> password_2(True, 20, "Elvy")
'True20Elvy'
>>> password_2(20, 40, "Elvy")
'Elvy60'
>>> password_2(20, "Elvy", 3)
'Elvy23'
>>> password_2(2, 3.333, "Elvy")
'Elvy6.67'
def password_2(var1, var2, var3):
countNumeric=0
# count numeric
if (isinstance(var1, int) or isinstance(var1, float)) and not(isinstance(var1, bool)):
countNumeric=countNumeric+1
if (isinstance(var2, int) or isinstance(var2, float)) and not(isinstance(var2, bool)):
countNumeric=countNumeric+1
if (isinstance(var3, int) or isinstance(var3, float)) and not(isinstance(var3, bool)):
countNumeric=countNumeric+1
if countNumeric<=1:
return str(var1)+str(var2)+str(var3)
# check if two are strings
if isinstance(var1, str) and isinstance(var2, str):
return var1+var2+str(var3)
elif isinstance(var2, str) and isinstance(var3, str):
return str(var1)+var2+var3
elif isinstance(var1, str) and isinstance(var3, str):
return var1+str(var2)+var3
# check if two are integer
if isinstance(var1, int) and isinstance(var2, int):
return var3+str(var1+var2)
elif isinstance(var1, int) and isinstance(var3, int):
return var2+str(var1+var3)
elif isinstance(var2, int) and isinstance(var3, int):
return var1+str(var2+var3)
# check if one is integer another one is float
if (isinstance(var1, int) and isinstance(var2, float)) or (isinstance(var2, int) and isinstance(var1, float)):
return var3+str(round(var1*var2,2))
elif (isinstance(var1, int) and isinstance(var3, float)) or (isinstance(var3, int) and isinstance(var1, float)):
return var2+str(round(var1*var3,2))
elif (isinstance(var2, int) and isinstance(var3, float)) or (isinstance(var3, int) and isinstance(var2, float)):
return var1+str(round(var2*var3,2))
# check if two are float
if (isinstance(var1, float) and isinstance(var2, float)):
return var3+str(round(var1*var2,2))
elif (isinstance(var1, float) and isinstance(var3, float)):
return var2+str(round(var1*var3,2))
elif (isinstance(var2, float) and isinstance(var3, float)):
return var1+str(round(var2*var3,2))
#driver code
# call the function and display result
print(password_2("Marina", "Langlois", "20"))
print(password_2("Marina", "Langlois", 20))
print(password_2("Elvy", True, 20))
print(password_2(True, 20, "Elvy"))
print(password_2(20, 40, "Elvy"))
print(password_2(20, "Elvy", 3))
print(password_2(5.2, 3.333, "Elvy"))
___________________________________________________________________
MarinaLanglois20
MarinaLanglois20
ElvyTrue20
True20Elvy
Elvy60
Elvy23
Elvy6.67
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.