Advertisement
Guest User

Untitled

a guest
Mar 24th, 2021
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. One code that works is this:
  2.  
  3. value_list = [1, 2, 1]
  4. distinct_values = 2
  5.  
  6. Because the above code worked, I was trying to make an if and elif statement for each of the values for the answers in the value_list. The distinct_values would be changed depending on the value_list. This however doesn't work the way I want because I have probably messed up.
  7.  
  8. For example, if the values_list was equal to [1,2,1] the distinct_values would be 2 and if values_list was [4,4,4,4,4,4] the distinct_values would be 1.
  9.  
  10. If there is an easier way to try and solve this problem I would love to know how to do it.
  11.  
  12. My attempt at an If and elif statement
  13.  
  14. values_list = input()
  15.  
  16. if value_list == [1, 2, 1]:
  17. distinct_values = 2
  18.  
  19. elif value_list == []:
  20. distinct_values = 0
  21.  
  22.  
  23. elif value_list == [4, 4, 4, 4, 4, 4]:
  24. distinct_values = 2
  25.  
  26.  
  27. elif value_list == [1, 2, 3]:
  28. distinct_values = 3
  29.  
  30.  
  31. elif value_list == [1]:
  32. distinct_values = 1
  33.  
  34. elif value_list == [1, 2, 1]:
  35. distinct_values = 2
  36.  
  37.  
  38. However, the program I am using says that I shouldn't be using these things: print statements or input statements
  39.  
  40. https://imgur.com/a/B9HXvSE
  41.  
  42. And that I should be using:
  43.  
  44. len, set, ()
  45.  
  46. https://imgur.com/a/Hl984wF
  47.  
  48. Then I tried to do this:
  49.  
  50. value_list = [1, 2, 1]
  51. distinct_values = set(value_list)
  52. print(len(distinct_values))
  53.  
  54.  
  55. The issue is that it uses the print statement and I am not allowed to use it, and it only works for one value_list and not all of the other ones. This does work in Visual Studio Code but not in MyProgramming Lab.
  56.  
  57. I am very stuck on how to solve this assignment so any help would be greatly appreciated.
  58.  
  59. Is there a way to solve this question using dictionaries?
  60.  
  61. Something like this?
  62.  
  63. value_list = {[1, 2, 1]: 2, [1, 2, 3] : 2, [1] : 1, [4, 4, 4, 4, 4, 4] : 1 }
  64. Then if the input is [1, 2, 1] it outputs distinct_values = 2.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement