Python: In my program, I want to get the value of each donation the user enter.
#enter number of doantions don = input("How many donations received? ") for i in range(0, len(don)): #Value of each separate donation given don[i] = input("Value of donation: ) This is my code so far but i keep getting an TypeError. pledges[i] = input("Value is: ") <- error line TypeError: 'str' object does not support item assignment If there are 3 donations. I want to get the value of each donation separately but,' I am not able to do that when i want to iterate through the elements. Is the method wrong, for trying to get the input of the elements in don, to get there values?
Wouldn't that problem line convert the (I assume) number entered to float? pledges[ i ] = float( input( "Value is: " ) ) I also don't see pledges declared anywhere: pledges = []
oh sorry i had changed the input variable to 'don' donations would be an integer and the values should be floats but i was testing to see if it would at least give me ex. 3 separate donation values because I entered that the user was given 3 donations.
``` don = input("How many donations received?") # you enter a number ``` Don is now an integer, so you can't use len(don) anymore, change that to don. Also you can just say range(don), which is the same as range(0, don) Now initialize your pledge amounts ``` pledge_amounts = [] ``` Now ask the user for amount and append that to pledge amounts. ``` for i in range(don): pledge_amounts.append(input("Value of donation: ")) ```
Join our real-time social learning platform and learn together with your friends!