简体   繁体   中英

Appending a list

I am trying to append the "result" variable into a new list called total_winnings but I get an error.

I managed to do it successfully for the total_stake but I get an error when I try use the same method for total_winnings .

I think it is because the "result" variable takes string input?

while True:

    add_selection =raw_input("Would you like to add a selection?")
    if add_selection == "Yes":

        selection = raw_input('Horse: ')
        print selection

        stake = float(raw_input('Stake: '))
        print stake

        odds = float(raw_input('Odds: '))
        print odds

        result = (raw_input('Result: '))
        if result == "Win":
            print stake * odds
        elif result == "Lose":
            print 0 * odds


        book = raw_input('Book: ')
        print book

        my_list=[selection,stake,odds,result,book]

        inputs.append(my_list)

        total_stake=[]
        for my_list in inputs:
            total_stake.append(my_list[1])
        print sum(total_stake)

        total_winnings = []
        for my_list in inputs:
            total_winnings.append(my_list[3])
        print sum(total_winnings)

        def looks_good(inputs):
            for i in inputs:    
                print i                       

    elif add_selection == "No":
        break

    looks_good(inputs)

Any help would be greatly appreciated.

The error you get is

TypeError: unsupported operand type(s) for +: 'int' and 'str'

The problem is that you are currently storing strings "Win" and "Lose" in my_list[3] whereas you mean to store stake * odds . When you do the function sum(my_list[3]) , it gives an error as it cannot sum up strings.

To fix the error, change the if else statement from:

if result == "Win":
   print stake * odds
elif result == "Lose":
   print 0 * odds

to:

if result == "Win":
   print stake * odds
   result = stake * odds
elif result == "Lose":
   print 0 * odds
   result = 0
result = (raw_input('Result: '))

result is a str (a string).

my_list=[selection,stake,odds,result,book]
...
for my_list in inputs:
    total_winnings.append(my_list[3])

my_list[3] is result , which is a str . If you print out total_winnings , I think you'll see something like ["Win", "Lose", "Lose"] .

print sum(total_winnings)

Now you're trying to sum those strings. This doesn't make sense, and presumably it gives you this error:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

I think you meant to do something like this:

result = (raw_input('Result: '))
if result == "Win":
    result = stake * odds
elif result == "Lose":
    result = 0

I can see several issues with your code.

First, defining a function ("def looks_good(inputs)") within a loop is not a good idea. Unless you're trying to do something relatively tricky, function definitions should be at the top level of your code, before other stuff.

Second, the variable named "inputs" is used in "inputs.append(my_list)" before it is defined anywhere.

Since you are using the same name ("inputs") in as an argument for the looks_good() function, you are setting up a situation where there are two different "inputs" variables - one inside the function and one outside - which is almost certainly a bad idea. Using different names for those different variable scopes may help.

Fix those issues first, and then see if your error message is any clearer. If it still isn't working, I'd suggest putting in lots of "print" statements to see exactly what is going on.

my_list[3] contains either Win of Lose ie a string value and summing over it won't work. You can do something like this

result = (raw_input('Result: '))
    result_val = stake*odds
    if result == "Win":
        print result_val
    elif result == "Lose":
        print 0

And while adding to my_list, simply add the result_val instead of result or in addition to result

my_list=[selection,stake,odds,result_val,book]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM