am_dot_com

IA 2022-10-17

Oct 17th, 2022 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. from my_dear_library \
  2. import askTheUserForNumber
  3.  
  4. i1 = askTheUserForNumber(
  5. pTheQuestion="Escreve um n1: "
  6. )
  7.  
  8. # named-params
  9. i2 = askTheUserForNumber(
  10. pTheQuestion="Escreve um n1: "
  11. )
  12.  
  13. strMsg:str="Eis os valores que recebi:\n"
  14. strMsg = strMsg + "i1: "+str(i1)+"\n"
  15. strMsg = strMsg + "i2: "+str(i2)+"\n"
  16.  
  17. strMsg = \
  18. "Eis os valores que recebi:\ni1: %d\ni2: %d"%\
  19. (i1, i2)
  20.  
  21. strMsg = \
  22. "Eis os valores que recebi:\ni1: {}\ni2: {}"\
  23. .format(i1, i2)
  24.  
  25. strMsg = \
  26. f"Eis os valores que recebi:\ni1: {i1}\ni2: {i2}"
  27.  
  28. print (strMsg)
  29.  
  30. """
  31. operadores aritméticos
  32. + adição
  33. - subtração
  34. * multiplicação
  35. / divisão real (com vírgula flutuante)
  36. // divisão inteira (não há parte fracionária)
  37. ** potência
  38. % resto da divisão inteira
  39. """
  40.  
  41. rAdd = i1+i2
  42. rSub = i1-i2
  43. rMult = i1*i2
  44. rDivR:float = i1/i2
  45. rDivI = i1//i2
  46. rPot = i1**i2
  47. rRemainder = i1%i2
  48.  
  49. strMsg = f"{i1}+{i2}={rAdd}\n"
  50. # cumulative concatenation
  51. strMsg += f"{i1}-{i2}={rSub}\n"
  52. strMsg += f"{i1}*{i2}={rMult}\n"
  53. strMsg += f"{i1}/{i2}={rDivR}\n"
  54. strMsg += f"{i1}//{i2}={rDivI}\n"
  55. strMsg += f"{i1}**{i2}={rPot}\n"
  56. strMsg += f"{i1}%{i2}={rRemainder}\n"
  57. print(strMsg)
  58.  
  59. """
  60. operadores booleanos
  61. George Boole
  62. True
  63. False
  64. e-lógico and
  65. ou-lógico or
  66. negação-lógica not
  67. """
  68. b1 = True and False # False (False é elemento absorvente do e)
  69. print(b1)
  70. b2 = True or False # True (True é elemento absorvente do out)
  71. print(b2)
  72. b3 = not b2 # False #notar que é operador (não está obrigado à notação das functions)
  73. print(b3)
  74.  
  75. *****************************************************************
  76.  
  77. # heron.py
  78. # exemplo de um "guess-and-check" algorithm
  79. # calcula raiz quadrada de qualquer nº, com qualquer precisão
  80.  
  81. import math
  82.  
  83. iWantToKnowTheSquareRootOf = x = 525
  84. guess = -88
  85. precision = 1/100000000
  86.  
  87. distance = (guess**2) - x
  88.  
  89. bGoodEnough = distance<=precision
  90.  
  91. iTeration = 0
  92. while (not bGoodEnough):
  93. iTeration+=1 # iTeration = iTeration+1
  94. guess = (guess + x/guess)/2
  95. distance = (guess ** 2) - x
  96. bGoodEnough = distance <= precision
  97.  
  98. strMsg=f"Iteration:{iTeration}\n"
  99. strMsg+=f"Current guess: {guess}\n"
  100. strMsg+=f"Current distance: {distance}\n"
  101. print(strMsg)
  102. # while
  103.  
  104. print("_.-^-."*10)
  105. strMsg = f"The Square Root of {x} is {math.fabs(guess)}\n"
  106. print(strMsg)
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment