I have many single float but how I can make then to be list of float?

Recommended Answers

All 7 Replies

If you have like this:

a = 2.2
b = 1.4
c = 2.7
d = 3.5
e = 42.8

one easy way is to simple create the list:

l=[a, b, c, d, e]
print l
'''output: [2.2, 1.4, 2.7, 3.5, 42.8]'''

that not work...all single float are in the same name

i tried

list(score)

for making it as list. It still not work

You are not making sense, you loose old value if you write over another value. Show all code for ths part.

Indeed, I was just pointing out that you can make a list if you have some variables.
What do you mean by:

I have many single float

and

that not work...all single float are in the same name

because you can store only 1 float value in a variable, you can't have more than 1 value per variable.

MOD EDIT: UPDATA ON CODE PASTING
Please, push code from menu and paste your source code.see

I knew that, I just said it to make it clear how it should be posted, meaning in an adequate form. I know that the CODE syntax is no longer avaible.

I have many single float but how I can make then to be list of float?

It's fairly easy. At the top of your program, you add thelist = list(). Then when you want to add a score to the list, you write thelist.append(score). Finally, add print(thelist) at the end.

commented: should solve it +14
commented: helpful +4

Here is a little more advanced code that checks your values you enter:

# numeric input function with range check
# works with Python25+ or Python3+

try:
    input = raw_input
except:
    pass

def get_num(low=0, high=100, ps="a number"):
    """
    the function will loop until a number within the given
    low and high range has been entered, returns a float
    """
    prompt = "Enter %s between %s and %s: " % (ps, low, high)
    while True:
        try:
            s = input(prompt)
            # accept only numeric characters that would be a float value
            if all(x in '1234567890.+-' for x in s):
                n = float(s)
                if low <= n <= high:
                    return n
        except:
            pass

# create an empty list
mylist = []
# ask for 10 numeric inputs and append to mylist
for k in range(10):
    s = "score #%d value" % (k+1)
    score = get_num(0,  200,  s)
    mylist.append(score)

print(mylist)

''' possible result (notice how score #5 is handled) >>
Enter score #1 value between 0 and 200: 180.2
Enter score #2 value between 0 and 200: 161
Enter score #3 value between 0 and 200: 177
Enter score #4 value between 0 and 200: 155
Enter score #5 value between 0 and 200: 300
Enter score #5 value between 0 and 200: 188.3
Enter score #6 value between 0 and 200: 187
Enter score #7 value between 0 and 200: 189
Enter score #8 value between 0 and 200: 191
Enter score #9 value between 0 and 200: 194
Enter score #10 value between 0 and 200: 199.5
[180.2, 161.0, 177.0, 155.0, 188.3, 187.0, 189.0, 191.0, 194.0, 199.5]
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.