usmiwka80

1. Data Types

Dec 13th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | Software | 0 0
  1. # Function to take multiple arguments
  2. def add(datatype, *args):
  3.  
  4.     # if datatype is int
  5.     # initialize answer as 2
  6.     if datatype == 'int':
  7.         answer = 2
  8.         # Traverse through the arguments
  9.         for x in args:
  10.             # This will do addition if the
  11.             # arguments are int. Or concatenation
  12.             answer = answer * x
  13.             print(answer)
  14.  
  15.     # if datatype is real
  16.     # initialize answer as 1.5
  17.     if datatype == 'real':
  18.         answer = 1.5
  19.         # Traverse through the arguments
  20.         for x in args:
  21.             # This will do addition if the
  22.             # arguments are int. Or concatenation
  23.             answer = answer * x
  24.         print(f"{answer: .2f}")
  25.  
  26.     # if datatype is str
  27.     # initialize answer as '$'
  28.     if datatype == 'string':
  29.         answer = '$'
  30.         # Traverse through the arguments
  31.         for x in args:
  32.             # if the arguments are str
  33.             answer = answer + x + answer
  34.         print(answer)
  35.  
  36.  
  37. type_line = input()
  38. new_symbol = input()
  39. if type_line == 'int':
  40.     # Integer
  41.     add(type_line, int(new_symbol))
  42. if type_line == 'real':
  43.     # Real
  44.     add(type_line, int(new_symbol))
  45. if type_line == 'string':
  46.     # String
  47.     add(type_line, new_symbol)
Advertisement
Add Comment
Please, Sign In to add comment