Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61026 > unrolled thread

Input without line break, is it possible?

Started bygeezle86@gmail.com
First post2013-12-04 07:38 -0800
Last post2013-12-05 08:21 -0700
Articles 10 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Input without line break, is it possible? geezle86@gmail.com - 2013-12-04 07:38 -0800
    Re: Input without line break, is it possible? Tim Chase <python.list@tim.thechases.com> - 2013-12-04 09:55 -0600
    Re: Input without line break, is it possible? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-04 16:04 +0000
    Re: Input without line break, is it possible? [correction] Tim Chase <tim@thechases.com> - 2013-12-04 10:07 -0600
    Re: Input without line break, is it possible? Chris Angelico <rosuav@gmail.com> - 2013-12-05 03:14 +1100
    Re: Input without line break, is it possible? Michael Torrie <torriem@gmail.com> - 2013-12-04 09:23 -0700
    Re: Input without line break, is it possible? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-04 16:27 +0000
    Re: Input without line break, is it possible? Chris Angelico <rosuav@gmail.com> - 2013-12-05 03:33 +1100
    Re: Input without line break, is it possible? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-04 16:36 +0000
    Re: Input without line break, is it possible? Michael Torrie <torriem@gmail.com> - 2013-12-05 08:21 -0700

#61026 — Input without line break, is it possible?

Fromgeezle86@gmail.com
Date2013-12-04 07:38 -0800
SubjectInput without line break, is it possible?
Message-ID<6f831d02-26bf-4e68-a1c6-a9c9e87db1dc@googlegroups.com>
The source code:

for i in range(8):
   n = input()

When we run it, consider the numbers below is the user input,

1
2
3
4
5
6
(and so forth)

my question, can i make it in just a single line like,

1 2 3 4 5 6 (and so forth)

Can I?

[toc] | [next] | [standalone]


#61029

FromTim Chase <python.list@tim.thechases.com>
Date2013-12-04 09:55 -0600
Message-ID<mailman.3568.1386172467.18130.python-list@python.org>
In reply to#61026
On 2013-12-04 07:38, geezle86@gmail.com wrote:
> for i in range(8):
>    n = input()
> 
> When we run it, consider the numbers below is the user input,
> 
> 1
> 2
> 3
> 4
> 5
> 6
> (and so forth)
> 
> my question, can i make it in just a single line like,
> 
> 1 2 3 4 5 6 (and so forth)

Not easily while processing the input one at a time.  You can,
however, read one line of input and then split it:

  s = input()
  bits = s.split()
  if len(bits) != 8:
    what_now("?")
  else:
    for bit in bits:
      do_something(bit)

You could make it a bit more robust with something like:

  answers = []
  while len(answers) < 8:
    s = input()
    answers.append(s.split())
  del answers[8:] # we only want 8, so throw away extras
  for answer in answers:
    do_something(answer)

which would at least ensure that you have 8 entries.

-tkc



[toc] | [prev] | [next] | [standalone]


#61030

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-12-04 16:04 +0000
Message-ID<mailman.3569.1386173067.18130.python-list@python.org>
In reply to#61026
On 04/12/2013 15:38, geezle86@gmail.com wrote:
> The source code:
>
> for i in range(8):
>     n = input()
>
> When we run it, consider the numbers below is the user input,
>
> 1
> 2
> 3
> 4
> 5
> 6
> (and so forth)
>
> my question, can i make it in just a single line like,
>
> 1 2 3 4 5 6 (and so forth)
>
> Can I?
>

Yes you can get them on a single line, see the response from Tim Chase. 
  But just to be crystal clear, are you aware that you're getting string 
representations of numbers, and not the numbers themselves?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#61031 — Re: Input without line break, is it possible? [correction]

FromTim Chase <tim@thechases.com>
Date2013-12-04 10:07 -0600
SubjectRe: Input without line break, is it possible? [correction]
Message-ID<mailman.3570.1386173184.18130.python-list@python.org>
In reply to#61026
On 2013-12-04 09:55, Tim Chase wrote:
> You could make it a bit more robust with something like:
> 
>   answers = []
>   while len(answers) < 8:
>     s = input()
>     answers.append(s.split())

this should be

  answers.extend(s.split())

instead of .append()

That's what I get for coding in my inbox rather than copy/pasting
from tested Python code.

-tkc


[toc] | [prev] | [next] | [standalone]


#61033

FromChris Angelico <rosuav@gmail.com>
Date2013-12-05 03:14 +1100
Message-ID<mailman.3572.1386173667.18130.python-list@python.org>
In reply to#61026
On Thu, Dec 5, 2013 at 3:04 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 04/12/2013 15:38, geezle86@gmail.com wrote:
>>
>> The source code:
>>
>> for i in range(8):
>>     n = input()
>
> Yes you can get them on a single line, see the response from Tim Chase.  But
> just to be crystal clear, are you aware that you're getting string
> representations of numbers, and not the numbers themselves?

Just to clarify, this is assuming that you're using Python 3. Geezle,
if you're using Python 2, you need to not use input() for anything -
use raw_input() instead, which will do what we're describing here.

I yearn for the day when nobody uses Python 2 any more so this doesn't
need to be asked.

ChrisA

[toc] | [prev] | [next] | [standalone]


#61036

FromMichael Torrie <torriem@gmail.com>
Date2013-12-04 09:23 -0700
Message-ID<mailman.3575.1386174218.18130.python-list@python.org>
In reply to#61026
On 12/04/2013 08:38 AM, geezle86@gmail.com wrote:
> my question, can i make it in just a single line like,
> 
> 1 2 3 4 5 6 (and so forth)
> 
> Can I?

Yes of course.  raw_input() is going to give you a string that you can
then parse any way you want.

[toc] | [prev] | [next] | [standalone]


#61037

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-12-04 16:27 +0000
Message-ID<mailman.3576.1386174486.18130.python-list@python.org>
In reply to#61026
On 04/12/2013 16:14, Chris Angelico wrote:
> On Thu, Dec 5, 2013 at 3:04 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
>> On 04/12/2013 15:38, geezle86@gmail.com wrote:
>>>
>>> The source code:
>>>
>>> for i in range(8):
>>>      n = input()
>>
>> Yes you can get them on a single line, see the response from Tim Chase.  But
>> just to be crystal clear, are you aware that you're getting string
>> representations of numbers, and not the numbers themselves?
>
> Just to clarify, this is assuming that you're using Python 3. Geezle,
> if you're using Python 2, you need to not use input() for anything -
> use raw_input() instead, which will do what we're describing here.

Good point, I saw input() and automatically assumed Python 3, what a 
sin!  The assumption obviously, not Python 3!!

>
> I yearn for the day when nobody uses Python 2 any more so this doesn't
> need to be asked.
>

I'm contemplating what it would be like migrating code from Python 1.x 
to Python 4.0, the fun and games that could be :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#61039

FromChris Angelico <rosuav@gmail.com>
Date2013-12-05 03:33 +1100
Message-ID<mailman.3578.1386174788.18130.python-list@python.org>
In reply to#61026
On Thu, Dec 5, 2013 at 3:27 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> I'm contemplating what it would be like migrating code from Python 1.x to
> Python 4.0, the fun and games that could be :)

I never used Python 1.x seriously, but when I went digging in one of
my OS/2 machines a while ago, I found several Pythons, including a
1.something. Fortunately for my task at hand, there was also a 2.5 or
2.6 or somesuch, which served my purposes :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#61041

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-12-04 16:36 +0000
Message-ID<mailman.3580.1386175207.18130.python-list@python.org>
In reply to#61026
On 04/12/2013 16:23, Michael Torrie wrote:
> On 12/04/2013 08:38 AM, geezle86@gmail.com wrote:
>> my question, can i make it in just a single line like,
>>
>> 1 2 3 4 5 6 (and so forth)
>>
>> Can I?
>
> Yes of course.  raw_input() is going to give you a string that you can
> then parse any way you want.
>

That's it lad, you're in detention for one hour.  You will write 
repeatedly "I should be selling Python 3 instead of Python 2" :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#61088

FromMichael Torrie <torriem@gmail.com>
Date2013-12-05 08:21 -0700
Message-ID<mailman.3607.1386256918.18130.python-list@python.org>
In reply to#61026
On 12/04/2013 09:36 AM, Mark Lawrence wrote:
> On 04/12/2013 16:23, Michael Torrie wrote:
>> On 12/04/2013 08:38 AM, geezle86@gmail.com wrote:
>>> my question, can i make it in just a single line like,
>>>
>>> 1 2 3 4 5 6 (and so forth)
>>>
>>> Can I?
>>
>> Yes of course.  raw_input() is going to give you a string that you can
>> then parse any way you want.
>>
> 
> That's it lad, you're in detention for one hour.  You will write 
> repeatedly "I should be selling Python 3 instead of Python 2" :)

Yup. Though if he is using Python 2, then input() is a real no-no.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web