12 Replies - 2292 Views - Last Post: 01 September 2011 - 03:07 AM Rate Topic: -----

#1 timber-cub   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-August 11

problem with math/input float on single line for multiple input

Posted 29 August 2011 - 12:04 AM

Hi everyone, new guy here :)

ok i have to create a program which multiplies numbers inputed in one line eg.
Enter numbers: 7 13 1 5
455



now the problem is getting the multiple numbers on the one line without starting a new line,
here is what I have tried
solver = int(raw_input("Enter numbers: "))
importmath

print solver * solver * solver


and i tried doing this for the new non-new line thing

solver = int(raw_input{("Enter numbers: ")})
importmath

print solver * solver * solver



unfortuanley neither of them would work and would start a new line everytime or would get an error ("valueerror"), can someone point out some tips to start me off?
thanks
David

Is This A Good Question/Topic? 0
  • +

Replies To: problem with math/input float on single line for multiple input

#2 Eric115   User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 698
  • Joined: 19-January 09

Re: problem with math/input float on single line for multiple input

Posted 29 August 2011 - 01:03 AM

Hi there,
first of all welcome to Dream In Code!
Secondly, if I were doing a program like this, as long as you are expecting valid input (e.g. no letters are going to be put in or something like that), you might be better taking the input as a string, splitting it and then changing the individual strings back to numbers to multiply.
Something like this for the input maybe:
import string
solver = str(raw_input{("Enter numbers: ")})

splitNumbs = solver.split(' ') #this breaks the numbers up at the spaces
#this returns a list of the numbers

numbs = []
#put all of the numbers into a list for multiplying later.
for i in splitNumbs:
    number = int(i)
    numbs.append(number)
#I will let you finish from here



Hope this helps!

This post has been edited by Eric115: 29 August 2011 - 01:04 AM

Was This Post Helpful? 2
  • +
  • -

#3 Brewer   User is offline

  • Awesome
  • member icon

Reputation: 182
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: problem with math/input float on single line for multiple input

Posted 29 August 2011 - 01:17 AM

Or if you want to be a boss you can use a list comprehension instead of a for loop. To follow Eric115's example:

numbers = [int(number) for number in raw_input().split()]


* Please take notice that my code is for Python 2.7. If you're running 3.x then you might need to import string.

This post has been edited by Brewer: 29 August 2011 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 timber-cub   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-August 11

Re: problem with math/input float on single line for multiple input

Posted 29 August 2011 - 01:47 AM

Hi
Thanks guys for the great support,
so in the end would it look like this?
cheers
import string
solver = str(raw_input(("Enter numbers: ")))

splitNumbs = solver.split(' ') 

numbs = []

for i in splitNumbs:
    number = int(i)
    numbs.append(number)


print numbs*i


Was This Post Helpful? 0
  • +
  • -

#5 Python_4_President   User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: problem with math/input float on single line for multiple input

Posted 29 August 2011 - 06:55 PM

View Posttimber-cub, on 29 August 2011 - 01:47 AM, said:

Hi
Thanks guys for the great support,
so in the end would it look like this?
cheers
import string
solver = str(raw_input(("Enter numbers: ")))

splitNumbs = solver.split(' ') 

numbs = []

for i in splitNumbs:
    number = int(i)
    numbs.append(number)


print numbs*i




I don't think so...

i is going to be == the last element in that list, and numbs is a list...

You can't add a list and an integer. like [one, two, three] + 5. That doesn't, for now anyway, make any sense.


numbs = []
for integer in raw_input("Enter Numbers: ").split():
    numbs.append(int(integer))

print [numb**3 for numb in numbs]


Was This Post Helpful? 0
  • +
  • -

#6 timber-cub   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-August 11

Re: problem with math/input float on single line for multiple input

Posted 30 August 2011 - 01:47 AM

hi
Thanks for that, so this is what i got, and im way off i think, what else do i need to try ? :/
thanks again everyone

import string
solver = str(raw_input(("Enter numbers: ")))

splitNumbs = solver.split(' ') 


numbs = []

for integer in raw_input().split():
    numbs.append(int(integer))

print [numb**3 for numb in numbs]



Was This Post Helpful? 0
  • +
  • -

#7 Brewer   User is offline

  • Awesome
  • member icon

Reputation: 182
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: problem with math/input float on single line for multiple input

Posted 30 August 2011 - 07:09 AM

View PostPython_4_President, on 29 August 2011 - 11:25 PM, said:

I don't think so...

i is going to be == the last element in that list, and numbs is a list...

You can't add a list and an integer. like [one, two, three] + 5. That doesn't, for now anyway, make any sense.


numbs = []
for integer in raw_input("Enter Numbers: ").split():
    numbs.append(int(integer))

print [numb**3 for numb in numbs]



This is bad advice. First off, if you tried to run your code, you should get an error on the last line because i does not exist in that scope. After the for loop finishes there is no such thing as the variable 'i'. Another thing is that Python_4_President states is that you can't add a non-list to a list. While this is true, I think what he was getting at is that you can't multiply a list which is, in fact, not true. If i had a value then print list * i would be a perfectly legitimate call. It would print list i times. Finally, the print statement at the bottom of this code prints a copy of numbs with each element raise to the third power .. I don't know why.

I would try this:

solution = 1 # We can't start with solution == 0 because then our solution would always be 0.

for number in raw_input("Enter numbers: ").split():
    solution *= numbers # Equivalent to solution = solution * numbers



What is important here isn't the list of numbers. Creating a list of numbers just wastes time and we can cut that step out entirely by multiplying solution by number and saving the result to solution.

This post has been edited by Brewer: 30 August 2011 - 07:09 AM

Was This Post Helpful? 1
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7233
  • View blog
  • Posts: 15,074
  • Joined: 16-October 07

Re: problem with math/input float on single line for multiple input

Posted 30 August 2011 - 08:12 AM

Test as you go.

>>> # this is silly
... # import string
... # raw input is a string, you really don't need the str
... # or the extra parens
... # solver = str(raw_input(("Enter numbers: ")))
... solver = raw_input("Enter numbers: ")
Enter numbers: 2 3 5 8 13
>>> # show what you got
... solver
'2 3 5 8 13'
>>> splitNumbs = solver.split(' ') 
>>> # show this too
... splitNumbs
['2', '3', '5', '8', '13']
>>> numbs = []
>>> # here's an error.  You have a perfectly good list 
... # called splitNumbs.  What are you doing with raw_input?
... # for integer in raw_input().split():
... # let's just loop for the list we already made
... # ( integer is a craptastic name... )
... for i in splitNumbs:
...     numbs.append(int(i))
... 
>>> # show what you got
... numbs
[2, 3, 5, 8, 13]
>>> # looks good, you can take it from here
...
>>>



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 timber-cub   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-August 11

Re: problem with math/input float on single line for multiple input

Posted 30 August 2011 - 04:29 PM

Hi
Thanks guys, I solved it in the end :)
with all your help
here is what i got to make it work
numbs = []
val = 1
for integer in raw_input("enter numbers : ").split():
    val *= int(integer)

print val


Was This Post Helpful? 0
  • +
  • -

#10 Hiram   User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: problem with math/input float on single line for multiple input

Posted 01 September 2011 - 02:53 AM

Well done, timber-cub!

For funzies, here's a one line solution:

reduce(lambda x, y: x*y, [int(z) for z in raw_input('Enter Numbers: ').split()] )



Your's is much easier on the eyes, I think :P
Was This Post Helpful? 1
  • +
  • -

#11 timber-cub   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-August 11

Re: problem with math/input float on single line for multiple input

Posted 01 September 2011 - 02:58 AM

View PostHiram, on 01 September 2011 - 02:53 AM, said:

Well done, timber-cub!

For funzies, here's a one line solution:

reduce(lambda x, y: x*y, [int(z) for z in raw_input('Enter Numbers: ').split()] )



Your's is much easier on the eyes, I think :P



Thanks :), yeah for me the hardest thing can be reading the code I am writing and figuring out where it is going,
My fellow Australian :)
Thanks

This post has been edited by timber-cub: 01 September 2011 - 02:59 AM

Was This Post Helpful? 0
  • +
  • -

#12 Hiram   User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: problem with math/input float on single line for multiple input

Posted 01 September 2011 - 03:03 AM

That's usually the hardest part when you're starting :) it gets easier the more you do it though!

I'll just point out, that the list you're declaring, numbs, wasn't used, so it's unnecessary :)

I'm in Brisbane, Queensland. Where abouts are you based?
Was This Post Helpful? 0
  • +
  • -

#13 timber-cub   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-August 11

Re: problem with math/input float on single line for multiple input

Posted 01 September 2011 - 03:07 AM

Hi
Thanks for the info and help on that, well I learn everyday :)
Yeah i'm in Sydney NSW, small world aye
Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1