I made a simple working craps game with point and everything, but I wanted to implement betting. The idea is that the player will input a bet amount and the amount will be deducted from the player wallet and added onto the bet. Afterwards if the player won, the bet is returned including the winnings.
I set up a variable that takes user input, I then append the variable to a list. Afterwards I set up two other variables that add everything in the lists together by: sum().
CODE:
bet_numbers = []
bet = sum(bet_numbers)
user_input = input("How much would you like to bet?")
bet_numbers.append(user_input)
user_input_2 = input("How much would you like to bet?")
bet_numbers.append(user_input_2)
print(bet_numbers)
print(bet)
for example
user_input = input("How much would you like to bet?")
200
user_input = input("How much would you like to bet?")
300
result:
[200,300]
0
bet = sum(int(b) for b in bet_numbers)
print(bet)
You've never summed up bet_number
after they were populated. Move the sum
just before printing bet
Also, you're taking str
inputs, so you need to convert them to int
before summing
Your code has some errors
You are summing an empty list, because you call bet = sum(bet_numbers)
right after you define bet_numbers = []
, and sum([])=0
Even if you were to call bet = sum(bet_numbers)
after asking for bets, it wouldn't have worked since your elements of bet_numbers
list will be strings, and you cannot sum strings. I tried it below and you can see it won't work
In [2]: sum(['2'+'2'])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-b6e099608f0b> in <module>
----> 1 sum(['2'+'2'])
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Taking all this in consideration, your code can be much simplified as
bet_numbers = []
#Run a for loop twice
for i in range(2):
#Ask for user input, convert to int and apppend to list
user_input = int(input("How much would you like to bet?"))
bet_numbers.append(user_input)
#Sum all items in bet_numbers lis
bet = sum(bet_numbers)
print(bet)
A sample output will be
How much would you like to bet?20
How much would you like to bet?30
50
Following the order of declaration bet is equal to 0 as it correctly did a sum of the bet_numbers items.
The solution would be to put bet = sum(bet_numbers)
after asking the inputs from the players
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.