0
start = int(input("Enter starting number: "))
stop = int(input("Enter number to stop viewing at: "))
numbers = [start:stop]

This gives me a syntax error. I can't even use numbers.append[start:stop] Any way I can use something like list slicing to input numbers into a list?

5
  • so, if a start=1 and stop=5, numbers=[1,2,3,4,5] ??? Commented May 27, 2016 at 9:15
  • The first syntax error is probably from this line: stop = int(input(Enter number to stop viewing at: ")) You're missing a quotation. Commented May 27, 2016 at 9:23
  • @glls yes, also if i could put in floats (at a rounded of 2 digits), that's be great Commented May 27, 2016 at 9:32
  • @Farhan.K oh sorry, I forgot that in the question, it's written correctly in the actual code. Commented May 27, 2016 at 9:33
  • you can, view the below info, it is pretty straightforward on pythons documentation Commented May 27, 2016 at 9:37

2 Answers 2

1

That is not the correct way to generate a list. If you want a list of numbers based on input you can use this:

start = int(input("Enter starting number: "))
stop = int(input("Enter number to stop viewing at: "))
numbers = [i for i in range(start,stop+1)]

You might want to read up on lists and range.

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

3 Comments

[i for i in range(start,stop+1)] is a complicated way to write list(range(start, stop + 1)).
@Matthias I wouldn't say it's complicated. It's basic list comprehension.
You're right, a list comprehension isn't exactly rocket science, but nevertheless it's more complicated than just using a call to list.
0

if my comment is the case, you can append items in your list, based on range as follows:

start = int(input("Enter starting number: "))
stop = int(input("Enter number to stop viewing at: "))
numbers=[]
for i in range(start, stop+1):
    numbers.append(i)

print list

for rounding digits:

round(number[, ndigits])

Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example, round(0.5) is 1.0 and round(-0.5) is -1.0).

from python orgs documentation.

1 Comment

or just, numbers = range(start, stop+1). Its better not to change the definition of list which is a keyword. I didnt notice that this is 3.x. Here, numbers = list(range(start, stop+1)) may be useful ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.