Re: [Tutor] The results of your email commands

2017-08-03 Thread Mats Wichmann
On 08/03/2017 10:21 AM, Alan Gauld via Tutor wrote:
> On 03/08/17 11:05, Abdur-Rahmaan Janhangeer wrote:
>> me i cooked up :...
> 
> Yes that works too, especially if you don;t need access
> to the individual prices later.
> 
> There are a couple of things to make it more Pythonic...
> 
>> x = True
>> sum = 0
>>
>> while (x==True):
>> a = input("input:")
>> if a =="exit":
>> x=False
> 
> You could replace that with
> 
> sum = 0
> while True:
>  a = input("input:")
>  if a =="exit":
>  break# exit the loop

I'd like to add a thought here... checking for a precise string as a
quit marker is fine, but it might be a little friendlier to accept
spelling variants (as long as none of them could be confused with valid
values... true in this case as you want numbers)... and also to let your
user know what you're expecting!

thus:

a = input("enter number ('exit' when done):)
if a in ('x', 'exit', 'Exit'):

> 
> and
> 
>> try:
>> sum += float(a)
>> except:
>> pass
> 
> That's a risky strategy because if there is
> any error other than the one you anticipate
> then you will never know about it and
> ignore it. You should always try to specify
> the error(s) if possible:
> 
> try:
>sum += float(a)
> except ValueError, TypeError:
>pass
> 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The results of your email commands

2017-08-03 Thread Alan Gauld via Tutor
On 03/08/17 11:05, Abdur-Rahmaan Janhangeer wrote:
> me i cooked up :...

Yes that works too, especially if you don;t need access
to the individual prices later.

There are a couple of things to make it more Pythonic...

> x = True
> sum = 0
> 
> while (x==True):
> a = input("input:")
> if a =="exit":
> x=False

You could replace that with

sum = 0
while True:
 a = input("input:")
 if a =="exit":
 break# exit the loop

and

> try:
> sum += float(a)
> except:
> pass

That's a risky strategy because if there is
any error other than the one you anticipate
then you will never know about it and
ignore it. You should always try to specify
the error(s) if possible:

try:
   sum += float(a)
except ValueError, TypeError:
   pass

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The results of your email commands

2017-08-03 Thread Abdur-Rahmaan Janhangeer
me i cooked up :

[warning : see code above better but i did it a bit differently]

x = True
sum = 0

while (x==True):
a = input("input:")
if a =="exit":
x=False
try:
sum += float(a)
except:
pass

print("sum :",sum)

On Thu, Aug 3, 2017 at 3:09 AM, Alan Gauld  wrote:

> I'[ve CCd the list, please use ReplyAll when responding to the list.
>
>
> On 02/08/17 22:13, Borisco Bizaro wrote:
> > Hi,am try to write a code that take input from user continuently
> > until key press it stop and give total amount user  enter.
>
> > while True:
> >   input ("enter another price :")
> > print"\n\n press 0 key to stop"
>
> The last line should probably be before the loop.
>
> You need to store the value somewhere, probably in a list
> of prices.
>
> Also you need to convert the input value to a number, probably
> a float in this case(although for a real system you'd probably
> use a Decimal or a multiplied integer for money.)
>
> You also need to check the value before you store it to see
> if its the last value (ie "0"). Something like
>
> value = input()
> if value == "0": break
> try: prices.append(float(value))
> except ValueError: print "you need a number"
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The results of your email commands

2017-08-02 Thread Alan Gauld
I'[ve CCd the list, please use ReplyAll when responding to the list.


On 02/08/17 22:13, Borisco Bizaro wrote:
> Hi,am try to write a code that take input from user continuently
> until key press it stop and give total amount user  enter.

> while True:
>   input ("enter another price :")
> print"\n\n press 0 key to stop"

The last line should probably be before the loop.

You need to store the value somewhere, probably in a list
of prices.

Also you need to convert the input value to a number, probably
a float in this case(although for a real system you'd probably
use a Decimal or a multiplied integer for money.)

You also need to check the value before you store it to see
if its the last value (ie "0"). Something like

value = input()
if value == "0": break
try: prices.append(float(value))
except ValueError: print "you need a number"


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor