Advertisement
lubattillah

challenge2_102

May 28th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. """
  2. a function that accepts the dimensions of a triangle and returns its area.
  3. """
  4. height=int(input("Please enter the dimension corresponding to the height of your triangle: "))
  5. base=int(input("Please enter the dimension corresponding to the base of your triangle: "))
  6. def area_triangle(a,b):
  7. area=0.5*height*base
  8. return area
  9. areaoftriangle=area_triangle(height,base)
  10. print(areaoftriangle)
  11.  
  12. """
  13. a function that accepts a string and returns the same string reversed.
  14. """
  15. string=input("Enter your string: ")
  16. def reversed_string(string):
  17. reversed=string[::-1]#Reversing a string starting from last position to first position and move backward by one step.
  18. return reversed
  19. reversed_str=reversed_string(string)
  20. print(reversed_str)
  21.  
  22. """
  23. a function that simulates a pair of dice being rolled n times and returns
  24. the number of occurrences of each score.
  25. """
  26. import random
  27. n=int(input("how many times you need to roll your dice? "))
  28. def dice(n):
  29. rolls = []
  30. for i in range(n):
  31. two_dice = random.randint(1, 6) , random.randint(1, 6)#list comprehension
  32. rolls.append(two_dice)
  33. occurrence = {score: rolls.count(score)for score in rolls}#counting elements in a list using count()
  34. return occurrence
  35.  
  36. print(dice(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement