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

Reply via email to