Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. 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.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Weighted Choice with input list

I need to make a weighted prediction based on the outcomes of this list however there are some problems im running into

Code:
from random import choices
import statistics
from time import sleep
threecount = 0
fourcount = 0
fivecount = 0
pthreecount = 0
pfourcount = 0
pfivecount = 0
lst = []
print("What did you obtain?")
for plays in range(5):

  listy = ["3", "4", "5"]
  x = int(input("3, 4 or 5 star: "))
  lst.append(x)
  sleep(.5)
 
          

print("List of what you obtained:", lst)
sleep(1)


y = str(statistics.mode(lst))
print("The most common star you got was " + y)
sleep(1)

threecounter = lst.count(3)
print("You obtained a 3 star", threecounter, "times")
sleep(1)

fourcounter = lst.count(4)
print("You obtained a 4 star", fourcounter, "times")
sleep(1)

fivecounter = lst.count(5)
print("You obtained a 5 star", fivecounter, "times")

if input == "3":
    pthreecount = pthreecount + 1
if input == "4":
    pfourcountt = pfourcount + 1
if input == "5":
    pfivecount = pfivecount + 1
x = choices(listy, weights=(pthreecount, pfourcount, pfivecount), k=3)
print(x)

Here's an example of the output of this code:

Code:
What did you obtain?
3, 4 or 5 star: 3
3, 4 or 5 star: 3
33, 4 or 5 star: 3
33, 4 or 5 star: 3
3, 4 or 5 star: 3
List of what you obtained: [3, 3, 3, 3, 3]
The most common star you got was 3
You obtained a 3 star 5 times
You obtained a 4 star 0 times
You obtained a 5 star 0 times
['5', '5', '5']

It shouldn't output {'5','5','5'} i want it to output a weighted choice based on how many times each one is chosen. Any advice?
 
Are you trying to do something like this?

Python:
# Do the imports
from random import choices
import statistics
# Define a dict to hold the individual counts
counter = {
    '3': 0,
    '4': 0,
    '5': 0
}
# Counter for the while loop
counting = 0
# A list to hold the numbers
numlist = []
# Start the loop
while counting < 5:
    # Using try except for basic error checking. /Only whole numbers can be entered
    try:
        num = int(input('Enter 3, 4 , or 5\n>> '))
    except ValueError:
        print('Only whole numbers are allowed')
    # Passed error checking continue
    else:
        # Using if elif and else for our numbers. if the number entered is anything other than
        # 3, 4, or 5, an error is printed
        if num == 3:
            print('you chose 3')
            counter['3'] += 1
            numlist.append(num)
            counting += 1
        elif num == 4:
            print('you chose 4')
            counter['4'] += 1
            numlist.append(num)
            counting += 1
        elif num == 5:
            print('you chose 5')
            counter['5'] += 1
            numlist.append(num)
            counting += 1
        else:
            print('Choices are: 3, 4, or 5')
            continue
# Print out all data
print(f'List of what you obtained {", ".join(str(n) for n in numlist)}')
print(f'The most common star you got was {statistics.mode(numlist)}')
for key, value in counter.items():
    print(f'You obtained a {key} star {value} times')
print(choices(numlist, weights=[n for n in numlist], k=3))

Output

Code:
Enter 3, 4 , or 5
>> 3
you chose 3     
Enter 3, 4 , or 5
>> 3
you chose 3     
Enter 3, 4 , or 5
>> 3
you chose 3     
Enter 3, 4 , or 5
>> 3
you chose 3     
Enter 3, 4 , or 5
>> 3
you chose 3
List of what you obtained 3, 3, 3, 3, 3
The most common star you got was 3
You obtained a 3 star 5 times
You obtained a 4 star 0 times
You obtained a 5 star 0 times
[3, 3, 3]
 
Are you trying to do something like this?

Python:
# Do the imports
from random import choices
import statistics
# Define a dict to hold the individual counts
counter = {
    '3': 0,
    '4': 0,
    '5': 0
}
# Counter for the while loop
counting = 0
# A list to hold the numbers
numlist = []
# Start the loop
while counting < 5:
    # Using try except for basic error checking. /Only whole numbers can be entered
    try:
        num = int(input('Enter 3, 4 , or 5\n>> '))
    except ValueError:
        print('Only whole numbers are allowed')
    # Passed error checking continue
    else:
        # Using if elif and else for our numbers. if the number entered is anything other than
        # 3, 4, or 5, an error is printed
        if num == 3:
            print('you chose 3')
            counter['3'] += 1
            numlist.append(num)
            counting += 1
        elif num == 4:
            print('you chose 4')
            counter['4'] += 1
            numlist.append(num)
            counting += 1
        elif num == 5:
            print('you chose 5')
            counter['5'] += 1
            numlist.append(num)
            counting += 1
        else:
            print('Choices are: 3, 4, or 5')
            continue
# Print out all data
print(f'List of what you obtained {", ".join(str(n) for n in numlist)}')
print(f'The most common star you got was {statistics.mode(numlist)}')
for key, value in counter.items():
    print(f'You obtained a {key} star {value} times')
print(choices(numlist, weights=[n for n in numlist], k=3))

Output

Code:
Enter 3, 4 , or 5
>> 3
you chose 3    
Enter 3, 4 , or 5
>> 3
you chose 3    
Enter 3, 4 , or 5
>> 3
you chose 3    
Enter 3, 4 , or 5
>> 3
you chose 3    
Enter 3, 4 , or 5
>> 3
you chose 3
List of what you obtained 3, 3, 3, 3, 3
The most common star you got was 3
You obtained a 3 star 5 times
You obtained a 4 star 0 times
You obtained a 5 star 0 times
[3, 3, 3]
Are you trying to do something like this?

Python:
# Do the imports
from random import choices
import statistics
# Define a dict to hold the individual counts
counter = {
    '3': 0,
    '4': 0,
    '5': 0
}
# Counter for the while loop
counting = 0
# A list to hold the numbers
numlist = []
# Start the loop
while counting < 5:
    # Using try except for basic error checking. /Only whole numbers can be entered
    try:
        num = int(input('Enter 3, 4 , or 5\n>> '))
    except ValueError:
        print('Only whole numbers are allowed')
    # Passed error checking continue
    else:
        # Using if elif and else for our numbers. if the number entered is anything other than
        # 3, 4, or 5, an error is printed
        if num == 3:
            print('you chose 3')
            counter['3'] += 1
            numlist.append(num)
            counting += 1
        elif num == 4:
            print('you chose 4')
            counter['4'] += 1
            numlist.append(num)
            counting += 1
        elif num == 5:
            print('you chose 5')
            counter['5'] += 1
            numlist.append(num)
            counting += 1
        else:
            print('Choices are: 3, 4, or 5')
            continue
# Print out all data
print(f'List of what you obtained {", ".join(str(n) for n in numlist)}')
print(f'The most common star you got was {statistics.mode(numlist)}')
for key, value in counter.items():
    print(f'You obtained a {key} star {value} times')
print(choices(numlist, weights=[n for n in numlist], k=3))

Output

Code:
Enter 3, 4 , or 5
>> 3
you chose 3    
Enter 3, 4 , or 5
>> 3
you chose 3    
Enter 3, 4 , or 5
>> 3
you chose 3    
Enter 3, 4 , or 5
>> 3
you chose 3    
Enter 3, 4 , or 5
>> 3
you chose 3
List of what you obtained 3, 3, 3, 3, 3
The most common star you got was 3
You obtained a 3 star 5 times
You obtained a 4 star 0 times
You obtained a 5 star 0 times
[3, 3, 3]
YES THIS IS EXACTLY WHAT I WANTED U HAVE NO IDEA HOW GRATEFUL I AMMMM!!! Now onto making a bar chart out of the data..........(is it too much to ask for help on this...perhaps a website where I could copy some code?) BUT THANK UUUU
 
Not really sure what data you going to plot but, you could give matplotlib a try.

Add to end of above code

Code:
stars = ['1', '2', '3', '4', '5']
plt.bar(stars, numlist, color=sample(colors, k=len(numlist)))
plt.show()
 

Attachments

  • Capture.PNG
    Capture.PNG
    16.7 KB · Views: 3
Last edited:
Are you trying to do something like this?

Python:
# Do the imports
from random import choices
import statistics
# Define a dict to hold the individual counts
counter = {
    '3': 0,
    '4': 0,
    '5': 0
}
# Counter for the while loop
counting = 0
# A list to hold the numbers
numlist = []
# Start the loop
while counting < 5:
    # Using try except for basic error checking. /Only whole numbers can be entered
    try:
        num = int(input('Enter 3, 4 , or 5\n>> '))
    except ValueError:
        print('Only whole numbers are allowed')
    # Passed error checking continue
    else:
        # Using if elif and else for our numbers. if the number entered is anything other than
        # 3, 4, or 5, an error is printed
        if num == 3:
            print('you chose 3')
            counter['3'] += 1
            numlist.append(num)
            counting += 1
        elif num == 4:
            print('you chose 4')
            counter['4'] += 1
            numlist.append(num)
            counting += 1
        elif num == 5:
            print('you chose 5')
            counter['5'] += 1
            numlist.append(num)
            counting += 1
        else:
            print('Choices are: 3, 4, or 5')
            continue
# Print out all data
print(f'List of what you obtained {", ".join(str(n) for n in numlist)}')
print(f'The most common star you got was {statistics.mode(numlist)}')
for key, value in counter.items():
    print(f'You obtained a {key} star {value} times')
print(choices(numlist, weights=[n for n in numlist], k=3))

Output

Code:
Enter 3, 4 , or 5
>> 3
you chose 3   
Enter 3, 4 , or 5
>> 3
you chose 3   
Enter 3, 4 , or 5
>> 3
you chose 3   
Enter 3, 4 , or 5
>> 3
you chose 3   
Enter 3, 4 , or 5
>> 3
you chose 3
List of what you obtained 3, 3, 3, 3, 3
The most common star you got was 3
You obtained a 3 star 5 times
You obtained a 4 star 0 times
You obtained a 5 star 0 times
[3, 3, 3]
Not really sure what data you going to plot but, you could give matplotlib a try.

Add to end of above code

Code:
stars = ['1', '2', '3', '4', '5']
plt.bar(stars, numlist, color=sample(colors, k=len(numlist)))
plt.show()
Thank you for this. It came up with some errors but i managed to get a working barchart!!

Code:
colors = ['red', 'green', 'blue']
stars = ['3 stars', '4 stars', '5 stars']
y_axis = [counter['3'], counter['4'], counter['5'] ]

plt.bar(stars, y_axis)
plt.title('BarChart')
plt.xlabel('Stars')
plt.ylabel('Number of Times Obtained')
plt.show()
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom