0

I need 10 floating-point value inputs but I don't know how to do it lol. This is the only thing that pops into my mind. Can somebody help me? It's for my homework.

input1, input2, input3, input4, input5, input6, input7, input8, input9, input10 = range[input(), input(), input(), input(), input(), input(), input(), input(), input(), input()]
  • 5
    Use a loop to ask 10 times and append them all to a list or dict. Making 10 variables for 10 values is already slightly ridiculous. What if you wanted to scale this up to 100 values? – Pranav Hosangadi Oct 16 '20 at 15:47
  • is it a while loop or a for loop? Sorry I'm still a rookie XD Yes, It's stressing me out already :( "Write a program that asks the user to input 10 floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs." This is the instruction, hard enough for me I guess.. – JustAPerson Oct 16 '20 at 15:49
  • ``input`` reads a line. It has no concept of floating point values, nor 10 floating point values. – MisterMiyagi Oct 16 '20 at 15:53
  • 2
    `for` and `while` loops do basically the same thing in different ways. Break it down into smaller problems! How do I ask for input? How do I check if the input is valid and ask again if it isn't? How do I do this 10 times? What data structure can hold a bunch of items? How do I find the sum of all these 10 values? Once you've broken the problem down like this, you will find it easier to answer the individual questions. If you get stuck, it's easier to find answers online for the smaller questions than the big one. @JustAPerson – Pranav Hosangadi Oct 16 '20 at 15:55

2 Answers2

1

Using split method and map function is the easiest way here.

input_line = input()
float_list = list(map(float, input_line.split()))

Expected input format: Any number (more than 1) of space separated floats or ints in a single line.

e.g.

1.1 2.2 3.3 4.4 5.5

Aniket Tiratkar
  • 798
  • 6
  • 16
0

Looking at your comment with the assignment instructions, I don't know why you think you need to do it in one expression. You definitely can't do exception handling in one expression. Instead, use a loop, and use a list instead of separate variables:

floats = []
for i in range(3):  # Fewer, just for example
    f = float(input(f'Enter float ({i}): '))
    floats.append(f)

print(floats)

Example run:

Enter float (0): 1.0
Enter float (1): 1.1
Enter float (2): 1.2
[1.0, 1.1, 1.2]

See also:

This code above just answers the question as asked. For the other parts of the assignment, see:

wjandrea
  • 28,235
  • 9
  • 60
  • 81