Question:
can we put this code in one line?

``````assignment1 = float(input("Assignment 1 result out of 100 \n"))
assignment2 = float(input("Assignment 2 result out of 100 \n"))
assignment3 = float(input("Assignment 3 result out of 100 \n"))
assignment4 = float(input("Assignment 4 result out of 100 \n"))
assignment5 = float(input("Assignment 5 result out of 100 \n"))
assignment6 = float(input("Assignment 6 result out of 100 \n"))
assignment7 = float(input("Assignment 7 result out of 100 \n"))
assignment8 = float(input("Assignment 8 result out of 100 \n"))
assignment9 = float(input("Assignment 9 result out of 100 \n"))
assignment10 = float(input("Assignment 10 result out of 100 \n"))
``````
1 Like

isnt this a assignment? if so i cant give the answer only hints

1 Like

You could, but it would be long.

``````assignment1, assignment2, assignment3, assignment4, assignment5, assignment6, assignment7, assignment8, assignment9, assignment10 = float(input("Assignment 1 result out of 100 \n")), float(input("Assignment 2 result out of 100 \n")), float(input("Assignment 3 result out of 100 \n")), float(input("Assignment 4 result out of 100 \n")), float(input("Assignment 5 result out of 100 \n")), float(input("Assignment 6 result out of 100 \n")), float(input("Assignment 7 result out of 100 \n")), float(input("Assignment 8 result out of 100 \n")), float(input("Assignment 9 result out of 100 \n")), float(input("Assignment 10 result out of 100 \n"))
``````

Why do you want it to be only 1 line anyway?

2 Likes

If you really want to use individual variables like that and not a list, try this: (it’s not a good solution)

``````for i in range(1, 11): globals()[f"assignment{i}"] = float(input(f"Assignment {i} result out of 100 \n"))
``````
5 Likes
``````assignments = [float(input(f"Assignment {i} result out of 100:\n")) for i in range(1, 11)]
``````
3 Likes

alternatively, a dict for better indexing:

``````assignments = {i: float(input(f"Assignment {i} result out of 100 \n")) for i in range(1, 11)}
``````
2 Likes