0

I'm having a problem of printing a string inside an input. I made a for loop for automatic numbering based on how many elements that the user wants to input.

list = []
n = int(input("How many elements you want to input: "))
for i in range(0, n):
   element = int(input(i+1))
   if n == element:
      print("hello")
      break
list.append(element)

For example, I inputted 3 in the number of elements. I want to make my program output be like this:

  1. input
  2. input
  3. input

(input is the user will type once the number is shown)

But my program looks like:

1input

2input

3input

I just want to work up with the design, but I don't know how to do it.

5
  • You only want to print n times the input given by the user is that right ? Because I suspect the 2nd for loop to be useless Commented Jul 28, 2021 at 8:15
  • I edited it. I did not notice that I have another loop Commented Jul 28, 2021 at 8:19
  • Do you want the "etc." to be printed too ? Commented Jul 28, 2021 at 8:20
  • Ah no. It is not needed. I just stay it like that so whatever the user input, I just want to print out the numbers. I just deleted the "etc" to avoid confusion Commented Jul 28, 2021 at 8:23
  • What do you mean by "work up"? Can you elaborate? Preferably, by editing (changing) your question, not here in comments (without "Edit:", "Update:", or similar - the question should appear as if it was written today). Commented Nov 22, 2021 at 14:37

5 Answers 5

3

What you need is called string formatting, and you might use .format by replacing

element = int(input(i+1))

using

element = int(input("{}. ".format(i+1)))

or using so-called f-strings (this requires Python 3.6 or newer):

element = int(input(f"{i+1}. "))

If you want to know more, I suggest reading realpython's guide.

Sign up to request clarification or add additional context in comments.

1 Comment

Tried the 2nd solution and it worked! Thank you so much
1

Try: input(str(i+1)+'. ')

This should append a point and a space to your Text. It converts the number of the input to a String, at which you can append another String, e.g. '. '.

1 Comment

This also worked! Tried to change it manually but when I copied, this work! Thank you!
1

You have to edit the input in the loop to something like this:

element = int(input(str(i+1) + ". "))

1 Comment

This worked! I just forgot something that caused an error but this also worked! Thank you!
1

You are close. Convert i+1 to a string and concatenate a . to it and accept input.

Note: Do not use list as a variable name. It is a Python reserved word.

lst = []
n = int(input("How many elements you want to input: \n"))
for i in range(n):
   element = int((input(str(i+1) + '. ')))
   if n == element:
      print("hello")
      break
lst.append(element)

How many elements you want to input: 
5
1. 1
2. 6
3. 7
4. 4
5. 6

1 Comment

Thank you the additional information about the list. I made a change and this worked!
0
n = int(input("How many elements you want to input: "))
for i in range(0, n):
      print(str(i+1) + ". " + str(n))

This should do.

I used the same code shape as yours, and that way it is easier for you to understand.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.