Re: The sum of ten numbers inserted from the user

2019-02-09 Thread Terry Reedy

On 2/9/2019 4:23 AM, Christian Gollwitzer wrote:

Am 08.02.19 um 09:58 schrieb ^Bart:

A colleague did:

total=0
for n in range(10):

 n= int(input("Enter a number: "))


Here, you are reusing "n" for two different things:
1. The loop index, indicating which number you ask for
2. The number entered from the user

This is avery bad thing.


It is not as bad as "int = int(input('Number? '))"




--
Terry Jan Reedy


--
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of ten numbers inserted from the user

2019-02-09 Thread Christian Gollwitzer

Am 08.02.19 um 09:58 schrieb ^Bart:

A colleague did:

total=0
for n in range(10):

     n= int(input("Enter a number: "))


Here, you are reusing "n" for two different things:
1. The loop index, indicating which number you ask for
2. The number entered from the user

This is avery bad thing.

Christian

--
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of ten numbers inserted from the user

2019-02-08 Thread ^Bart

x = 0
for jnk in range(10):
x += int(input("Enter a number: ")
print(x)


It works, there's a missed )

A colleague did:

total=0
for n in range(10):

n= int(input("Enter a number: "))
total=total+n

print(total)

I understood your code is more clean!

^Bart


--
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of ten numbers inserted from the user

2019-02-07 Thread Ben Bacarisse
Grant Edwards  writes:

> On 2019-02-07, Ben Bacarisse  wrote:
>> Ian Clark  writes:
>>
>>> This is my whack at it, I can't wait to hear about it being the wrong big o
>>> notation!
>>>
>>> numbers=[]
>>>
>>> while len(numbers) < 10:
>>> try:
>>> chip = int(input('please enter an integer: '))
>>> except ValueError:
>>> print('that is not a number, try again')
>>> else:
>>> numbers.append(chip)
>>>
>>> print(sum(numbers))
>>
>> Why would you not keep a running total, rather than list of the numbers?
>> I've not been following assiduously, so maybe I missed some other
>> requirement...
>
> Because you think it likely that tomorrow the marketing people are
> going to say "oh, and we want to know the min, max, mode, median,
> standard deviation, and count of odd vs. even.
>
> OTOH, they may instead say, "Oh, it's not 10 numbers, it's 10 million
> numbers."

Sure.  I can think of reasons for all sorts of designs.  Has there been
any hint?  It looks like a beginners' exercise with little extra to go
on.

-- 
Ben.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of ten numbers inserted from the user

2019-02-07 Thread Ben Bacarisse
Bart  writes:

> On 07/02/2019 20:45, Ben Bacarisse wrote:
>> Ian Clark  writes:
>>
>>> This is my whack at it, I can't wait to hear about it being the wrong big o
>>> notation!
>>>
>>> numbers=[]
>>>
>>> while len(numbers) < 10:
>>>  try:
>>>  chip = int(input('please enter an integer: '))
>>>  except ValueError:
>>>  print('that is not a number, try again')
>>>  else:
>>>  numbers.append(chip)
>>>
>>> print(sum(numbers))
>>
>> Why would you not keep a running total, rather than list of the numbers?
>> I've not been following assiduously, so maybe I missed some other
>> requirement...
>
> Because it separates the two tasks: (A) capture N values; (B) perform
> some operation on those values, in this case summing (and taking
> advantage here of the built-in sum()).

Sure.  Did I miss some hint that this program might need to be more
flexible like that?  It looks like a beginners' exercise.

If the purpose is to be flexible, why is an array the right option rather
than, say, accumulating a value over an iterable?  You need some idea of
the future direction or what the exercise is intended to reinforce.


-- 
Ben.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of ten numbers inserted from the user

2019-02-07 Thread Grant Edwards
On 2019-02-07, Ben Bacarisse  wrote:
> Ian Clark  writes:
>
>> This is my whack at it, I can't wait to hear about it being the wrong big o
>> notation!
>>
>> numbers=[]
>>
>> while len(numbers) < 10:
>> try:
>> chip = int(input('please enter an integer: '))
>> except ValueError:
>> print('that is not a number, try again')
>> else:
>> numbers.append(chip)
>>
>> print(sum(numbers))
>
> Why would you not keep a running total, rather than list of the numbers?
> I've not been following assiduously, so maybe I missed some other
> requirement...

Because you think it likely that tomorrow the marketing people are
going to say "oh, and we want to know the min, max, mode, median,
standard deviation, and count of odd vs. even.

OTOH, they may instead say, "Oh, it's not 10 numbers, it's 10 million
numbers."

-- 
Grant Edwards   grant.b.edwardsYow! I selected E5 ... but
  at   I didn't hear "Sam the Sham
  gmail.comand the Pharoahs"!

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of ten numbers inserted from the user

2019-02-07 Thread Ben Bacarisse
Ian Clark  writes:

> This is my whack at it, I can't wait to hear about it being the wrong big o
> notation!
>
> numbers=[]
>
> while len(numbers) < 10:
> try:
> chip = int(input('please enter an integer: '))
> except ValueError:
> print('that is not a number, try again')
> else:
> numbers.append(chip)
>
> print(sum(numbers))

Why would you not keep a running total, rather than list of the numbers?
I've not been following assiduously, so maybe I missed some other
requirement...


-- 
Ben.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: The sum of ten numbers inserted from the user

2019-02-07 Thread Schachner, Joseph
I just realized that input has changed in Python 3 and I was using Python 
2.7.13 with from __future__ import print_function and some others, but not that.
In Python 3 the int( ) or float( ) cast is necessary because input( ) does what 
raw_input( ) did in Python 2; raw_input( ) name is therefore removed.

--- Joe S.

From: Ian Clark 
Sent: Thursday, February 7, 2019 1:27 PM
To: Schachner, Joseph 
Cc: python-list@python.org
Subject: Re: The sum of ten numbers inserted from the user

This is my whack at it, I can't wait to hear about it being the wrong big o 
notation!

numbers=[]

while len(numbers) < 10:
try:
chip = int(input('please enter an integer: '))
except ValueError:
print('that is not a number, try again')
else:
numbers.append(chip)

print(sum(numbers))


On Thu, Feb 7, 2019 at 10:23 AM Schachner, Joseph 
mailto:joseph.schach...@teledyne.com>> wrote:
Well of course that doesn't work.  For starters, x is an int or a float value.  
After the loop It holds the 10th value.  It might hold 432.7 ...   It is not a 
list.
The default start for range is 0.  The stop value, as you already know, is not 
part of the range.  So I will use range(10).

In the second loop, I think you thought x would be a list and you I'm sure you 
wanted to do
for y in x:
but instead you did
for y in range(x)
 and remember x might be a very large number.So the second loop would loop 
that many times, and each pass it would assign y (which has first value of 0 
and last value of whatever x-1 is) to sum.  Even though its name is "sum" it is 
not a sum.  After the loop it would hold x-1.  You wanted to do  sum += y.
Or sum = sum + y.   I prefer the former, but that could reflect my history as a 
C and C++ programmer.

And then you printed y, but you really want to print sum  (assuming that sum 
was actually the sum).

Putting all of the above together, you want to do this:

vallist =[] # an empty list, so we can append to it
for n in range(10):
x = input("Insert a number: ")# get 1 number into x
vallist.append(x)  # append it to our list

# Now vallist holds 10 values

sum = 0   # No need to start sum as a float, in Python if we add a float to an 
integer the result will be float. It doesn't matter in the program if sum is 
int or float.
for y in vallist:
 sum += y

print( "The sum is: ", sum)



-Original Message-
From: ^Bart mailto:gabriele1nos...@hotmail.com>>
Sent: Thursday, February 7, 2019 6:30 AM
To: python-list@python.org
Subject: The sum of ten numbers inserted from the user

I thought something like it but doesn't work...

for n in range(1, 11):
 x = input("Insert a number: ")

for y in range(x):
 sum = y

 print ("The sum is: ",y)

--
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of ten numbers inserted from the user

2019-02-07 Thread Ian Clark
This is my whack at it, I can't wait to hear about it being the wrong big o
notation!

numbers=[]

while len(numbers) < 10:
try:
chip = int(input('please enter an integer: '))
except ValueError:
print('that is not a number, try again')
else:
numbers.append(chip)

print(sum(numbers))


On Thu, Feb 7, 2019 at 10:23 AM Schachner, Joseph <
joseph.schach...@teledyne.com> wrote:

> Well of course that doesn't work.  For starters, x is an int or a float
> value.  After the loop It holds the 10th value.  It might hold 432.7 ...
>  It is not a list.
> The default start for range is 0.  The stop value, as you already know, is
> not part of the range.  So I will use range(10).
>
> In the second loop, I think you thought x would be a list and you I'm sure
> you wanted to do
> for y in x:
> but instead you did
> for y in range(x)
>  and remember x might be a very large number.So the second loop would
> loop that many times, and each pass it would assign y (which has first
> value of 0 and last value of whatever x-1 is) to sum.  Even though its name
> is "sum" it is not a sum.  After the loop it would hold x-1.  You wanted to
> do  sum += y.Or sum = sum + y.   I prefer the former, but that could
> reflect my history as a C and C++ programmer.
>
> And then you printed y, but you really want to print sum  (assuming that
> sum was actually the sum).
>
> Putting all of the above together, you want to do this:
>
> vallist =[] # an empty list, so we can append to it
> for n in range(10):
> x = input("Insert a number: ")# get 1 number into x
> vallist.append(x)  # append it to our list
>
> # Now vallist holds 10 values
>
> sum = 0   # No need to start sum as a float, in Python if we add a float
> to an integer the result will be float. It doesn't matter in the program if
> sum is int or float.
> for y in vallist:
>  sum += y
>
> print( "The sum is: ", sum)
>
>
>
> -Original Message-
> From: ^Bart 
> Sent: Thursday, February 7, 2019 6:30 AM
> To: python-list@python.org
> Subject: The sum of ten numbers inserted from the user
>
> I thought something like it but doesn't work...
>
> for n in range(1, 11):
>  x = input("Insert a number: ")
>
> for y in range(x):
>  sum = y
>
>  print ("The sum is: ",y)
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: The sum of ten numbers inserted from the user

2019-02-07 Thread Schachner, Joseph
Well of course that doesn't work.  For starters, x is an int or a float value.  
After the loop It holds the 10th value.  It might hold 432.7 ...   It is not a 
list.
The default start for range is 0.  The stop value, as you already know, is not 
part of the range.  So I will use range(10).

In the second loop, I think you thought x would be a list and you I'm sure you 
wanted to do 
for y in x:
but instead you did 
for y in range(x)
 and remember x might be a very large number.So the second loop would loop 
that many times, and each pass it would assign y (which has first value of 0 
and last value of whatever x-1 is) to sum.  Even though its name is "sum" it is 
not a sum.  After the loop it would hold x-1.  You wanted to do  sum += y.
Or sum = sum + y.   I prefer the former, but that could reflect my history as a 
C and C++ programmer.

And then you printed y, but you really want to print sum  (assuming that sum 
was actually the sum).

Putting all of the above together, you want to do this:

vallist =[] # an empty list, so we can append to it
for n in range(10):
x = input("Insert a number: ")# get 1 number into x
vallist.append(x)  # append it to our list

# Now vallist holds 10 values

sum = 0   # No need to start sum as a float, in Python if we add a float to an 
integer the result will be float. It doesn't matter in the program if sum is 
int or float.
for y in vallist:
 sum += y

print( "The sum is: ", sum)



-Original Message-
From: ^Bart  
Sent: Thursday, February 7, 2019 6:30 AM
To: python-list@python.org
Subject: The sum of ten numbers inserted from the user

I thought something like it but doesn't work...

for n in range(1, 11):
 x = input("Insert a number: ")

for y in range(x):
 sum = y

 print ("The sum is: ",y)

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of ten numbers inserted from the user

2019-02-07 Thread Joel Goldstick
On Thu, Feb 7, 2019 at 6:31 AM ^Bart  wrote:
>
> I thought something like it but doesn't work...
>
> for n in range(1, 11):
>  x = input("Insert a number: ")

The above, keeps replacing x with each input value.  You don't want
that.  Think about appending the input value to a list
>
> for y in range(x):
>  sum = y
>
Here again you are replacing sum with each value of y.  And think
further about what the value of y is for each iteration.

>  print ("The sum is: ",y)

You should really be asking this sort of question in the python-tutor
mailing list.  You should also use print function in your loops to
learn what is really going on.



> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list