quick python question

Soldato
Joined
30 Nov 2005
Posts
13,916
value = [raw_input()]
amounts.append(value)

# user will input a few times and amounts will look something like [23.5,34.4,88.9,3.2,9.5]

i want to convert the amounts list to a list of floats rather than strings
 
Python variable data types are set by the type of data being applied to them. In the case of 'raw_input', you'll be appending a string type to your 'amounts' list. You'll need to cast to a float if you want it as thus...
 
ref = raw_input('please enter reference number : ')
tint = input('enter the number of items: ')
total = input('Enter the required total : ')
i = 1
id = []
amounts = []
while i <= tint :
print 'Enter item : ' ,
tempID = [raw_input()]
id.append(tempID)
print 'Enter amount: ',
value = [raw_input()]
amounts.append(value)
i = i +1


#now i want to get the sum of amounts divide by 100 and times it by total#
#and place back into another list.
 
Last edited:
amount.append(float(value))

Also, you almost never need to use a counter in python as you can use for loops. In the above example you can simply do:- for i in range(int(tint)) or you could do:- while len(id) <= tint and len(amounts) <= tint:

You can also just use the input directly in the append without saving it to a variable first eg.

amount.append(float(raw_input()))
 
Last edited:
many thanks that worked great,

whats wrong with this

for i in range(int(tint)):
calc.append(float(amounts/sum(amounts)*total))
 
What error is the Python console throwing at you? I can't tell but are you sure you're casting all the char*'s to floats before you're attempting arithmetic functions on them?

Also you should use the forums's 'code' function; Python code blocks are defined by tabification so it's difficult to tell whether you're doing it correctly or not.
 
Back
Top Bottom