Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Help with simulator game/ counting data

This is an extract of my code that is causing problems. The overall code counts up the amount of time the user obtains an item (can be used for many games but im using 3, 4, 5 stars as generic example) However i added a simulation mode where the player can choose to get random items from the computer which isnt going well...

Code:
if opponentChoice == "c": #simulation
    Com1 = "Computer1"
    weighted_list1 = random.choices(com1List, weights=(10, 3, 1), k=maxplays)
    print("Computer1 obtains...", weighted_list1)
    Com2 = "Computer2"
    weighted_list2 = random.choices(com2List, weights=(10, 3, 1), k=maxplays)
    print("Computer2 obtains...", weighted_list2)
    a = 3
    b = 4
    c = 5
    if a in weighted_list1:
      comthree = comthree + 1
    if b in weighted_list1:
      comfour = comfour + 1
    if c in weighted_list1:
      comfive = comfive + 1
    if a in weighted_list2:
      comthree2 = comthree2 + 1
    if b in weighted_list2:
      comfour2 = comfour2 + 1
    if c in weighted_list2:
      comfive2 = comfive2 + 1
      

  print("Table of items obtained")
  print("     ", Com1, "3 Stars obtained:", comthree)
  print("     ", Com1, "4 Stars obtained:", comfour)
  print("     ", Com1, "5 Stars obtained:", comfive)
  print()
  print("     ", Com2, "3 Stars obtained:", comthree2)
  print("     ", Com2, "4 Stars obtained:", comfour2)
  print("     ", Com2, "5 Stars obtained:", comfive2)

The output is:

Computer1 obtains... [3, 4]
Computer2 obtains... [3, 3]

Table of items obtained
Computer1 3 Stars obtained: 1
Computer1 4 Stars obtained: 1
Computer1 5 Stars obtained: 0

Computer2 3 Stars obtained: 1
Computer2 4 Stars obtained: 0
Computer2 5 Stars obtained: 0

The problem is that it only counts up the first number so whenever there is more than one 3 star etc it doesn't register it. I'm assuming I need to change the if statements? Sorry i'm not good at explaining the problem but i hope you can understand. The next step will be to to record all the data obtained and adding it to a cvs file which i'm also not sure how to do so if anyone knows how to do that, that'd be helpful. thank u!!
 
This is an extract of my code that is causing problems. The overall code counts up the amount of time the user obtains an item (can be used for many games but im using 3, 4, 5 stars as generic example) However i added a simulation mode where the player can choose to get random items from the computer which isnt going well...

Code:
if opponentChoice == "c": #simulation
    Com1 = "Computer1"
    weighted_list1 = random.choices(com1List, weights=(10, 3, 1), k=maxplays)
    print("Computer1 obtains...", weighted_list1)
    Com2 = "Computer2"
    weighted_list2 = random.choices(com2List, weights=(10, 3, 1), k=maxplays)
    print("Computer2 obtains...", weighted_list2)
    a = 3
    b = 4
    c = 5
    if a in weighted_list1:
      comthree = comthree + 1
    if b in weighted_list1:
      comfour = comfour + 1
    if c in weighted_list1:
      comfive = comfive + 1
    if a in weighted_list2:
      comthree2 = comthree2 + 1
    if b in weighted_list2:
      comfour2 = comfour2 + 1
    if c in weighted_list2:
      comfive2 = comfive2 + 1
     

  print("Table of items obtained")
  print("     ", Com1, "3 Stars obtained:", comthree)
  print("     ", Com1, "4 Stars obtained:", comfour)
  print("     ", Com1, "5 Stars obtained:", comfive)
  print()
  print("     ", Com2, "3 Stars obtained:", comthree2)
  print("     ", Com2, "4 Stars obtained:", comfour2)
  print("     ", Com2, "5 Stars obtained:", comfive2)

The output is:

Computer1 obtains... [3, 4]
Computer2 obtains... [3, 3]

Table of items obtained
Computer1 3 Stars obtained: 1
Computer1 4 Stars obtained: 1
Computer1 5 Stars obtained: 0

Computer2 3 Stars obtained: 1
Computer2 4 Stars obtained: 0
Computer2 5 Stars obtained: 0

The problem is that it only counts up the first number so whenever there is more than one 3 star etc it doesn't register it. I'm assuming I need to change the if statements? Sorry i'm not good at explaining the problem but i hope you can understand. The next step will be to to record all the data obtained and adding it to a cvs file which i'm also not sure how to do so if anyone knows how to do that, that'd be helpful. thank u!!
Hi there,
It sounds like you might need to count the number of duplicates in your list ;)
which can be done with something like this
Python:
my_dict = {i:MyList.count(i) for i in MyList}
where i is the current item in the list, and MyList is the list you are looking to count.


For reference:
 
Hi there,
It sounds like you might need to count the number of duplicates in your list ;)
which can be done with something like this
Python:
my_dict = {i:MyList.count(i) for i in MyList}
where i is the current item in the list, and MyList is the list you are looking to count.


For reference:
Thank you so much for this, I can't edit my coding at the moment since I'm busy but i'll let you know how it turns out !!
 
Back
Top Bottom