PrQ PrQ - 1 year ago 72
Python Question

Adding random numbers to a variable

I'm looking for an output like this

Computer Choice: 5
Total: 5
Continue? Yes or No
Computer Choice:2
Total: 7

It adds up the random numbers every time it's created. This is the part I'm trying to get to work:

if player_bet <= Player.total_money_amount:

import random
computer_choice = random.randint(1, 5) # Creates random number
computer_total =+ computer_choice # Does not work. Also used += same result
print('Computer choice: ', computer_choice)
print('Total: ', computer_total)

player_yes_or_no = input('Continue? Yes or No')
if player_yes_or_no == 'Yes':

Current output

Computer Choice: 5
Total: 5
Continue? Yes or No
Computer Choice: 2
Total: 2

As you can see it does not add up the random int that where created.
If I do += it gives an error

Edit: I get the same output when I do

computer_total = 0
computer_total += computer_choice

Answer Source

Change =+ to +=. The way you currently have it you are reassigning the variable computer_total to the value of computer_choice rather than adding them. Also, make sure to initialize computer_total before you start your loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download