Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #55391
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'elif': 0.05; 'assign': 0.07; 'duplicate': 0.07; 'none:': 0.07; 'sys': 0.07; 'subject:help': 0.08; '%s"': 0.09; 'definition,': 0.09; 'except:': 0.09; 'input,': 0.09; 'logic': 0.09; 'main()': 0.09; 'properly.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; '"-":': 0.16; '"programming': 0.16; '999': 0.16; 'exit()': 0.16; 'inputs': 0.16; 'item)': 0.16; 'main():': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sys.exit(1)': 0.16; 'true:': 0.16; 'values?': 0.16; 'applies': 0.16; 'exception': 0.16; 'wed,': 0.18; 'module': 0.19; 'passing': 0.19; 'programming': 0.22; 'import': 0.22; 'print': 0.22; 'error': 0.23; 'module,': 0.24; 'url:home': 0.24; 'second': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'skip:c 30': 0.32; 'worked': 0.33; 'third': 0.33; 'comment': 0.34; '"the': 0.34; 'could': 0.34; 'but': 0.35; 'add': 0.35; 'really': 0.36; 'charset:us-ascii': 0.36; 'too': 0.37; 'two': 0.37; 'level': 0.37; 'received:76': 0.38; 'work?': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'previous': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'skip:y 20': 0.60; 'skip:z 20': 0.60; 'numbers': 0.61; 'simple': 0.61; 'first': 0.61; 'more': 0.64; 'close': 0.67; 'invalid': 0.68; 'repeat': 0.74; '321': 0.84; 'again?': 0.84; 'max.': 0.84; 'lazy': 0.91; '2013': 0.98 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
| Subject | Re: Efficency help for a Calculator Program |
| Date | Wed, 02 Oct 2013 19:47:15 -0400 |
| Organization | IISS Elusive Unicorn |
| References | <77bde84a-b0ce-4061-bb2e-e6cd23f4282c@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | adsl-76-249-20-155.dsl.klmzmi.sbcglobal.net |
| X-Newsreader | Forte Agent 6.00/32.1186 |
| X-No-Archive | YES |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.661.1380757646.18130.python-list@python.org> (permalink) |
| Lines | 178 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1380757646 news.xs4all.nl 15891 [2001:888:2000:d::a6]:44468 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:55391 |
Show key headers only | View raw
On Wed, 2 Oct 2013 03:44:24 -0700 (PDT), JonDoe297
<vignesh.harikrishna@gmail.com> declaimed the following:
>
>global repeat
This does nothing at the top level of a module
>repeat=1
>def main():
> c=int(raw_input("How many numbers do you want to work? (Min. 2 Max. 3) "))
You don't handle the case where someone enter a non-integer item
> if c==2:
> x=int(raw_input("Enter the first number to be worked "))
> y=int(raw_input("Enter the second number to be worked "))
> elif c==3:
> x=int(raw_input("Enter the first number to be worked "))
> y=int(raw_input("Enter the second number to be worked "))
> z=int(raw_input("Enter the third number to be worked "))
You duplicate the first two inputs -- yet by your definition, two is
the minimum. And since 3 is the maximum...
x = int(....)
y = int(....)
if c == 3:
z = int(....)
> else:
> print "Invalid input.";raw_input("Press <enter> to close this window");exit()
> p=int(raw_input("Do you want to divide, subtract, add or multiply these numbers? (1=divide, 2=subtract, 3=add, 4=multiply) "))
> if p==1 and c==2:
> print "The result is : ",x/y
> repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 No=2 "))
> if repeat==1:
> main()
> elif p==1 and c==3:
> print "The result is : ",x/y/z
> repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 No=2 "))
> if repeat==1:
> main()
> elif p==2 and c==2:
> print "The result is : ",x-y
> repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 No=2 "))
> if repeat==1:
> main()
> elif p==2 and c==3:
> print "The result is : ",x-y-z
> repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 No=2 "))
> if repeat==1:
> main()
> elif p==3 and c==2:
> print "The result is : ",x+y
> repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 No=2 "))
> if repeat==1:
> main()
> elif p==3 and c==3:
> print "The result is : ",x+y+z
> repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 No=2 "))
> if repeat==1:
> main()
> elif p==4 and c==2:
> print "The result is : ",x*y
> repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 No=2 "))
> if repeat==1:
> main()
> elif p==4 and c==3:
> print "The result is : "+str(x*y*z)
> repeat=int(raw_input("Do you want to calculate for more numbers? Yes=1 No=2 "))
> if repeat==1:
> main()
> else:
> repeat=int(raw_input("Invalid Input. Please read instructions properly. Would you like to try again? Yes=1 No=2 "))
> if repeat==1:
> main()
> else:
> exit()
>main()
>
Ack! way too many conditionals... Condense out the logic -- since you
apply the same operator to all data you just need a way to pass the
operator to a function that applies it to all the data.
-=-=-=-=-=-
import sys
while True:
try:
numItems = int(raw_input("\n\nHow many values? "))
except: #naked exception is not really good programming
print "Invalid input, exiting..."
sys.exit(1)
print numItems
items = []
while len(items) < numItems:
try:
item = int(raw_input("Enter item #%s> " % (len(items) + 1)))
items.append(item)
except: #see previous comment
print "Invalid input, try again"
oper = None
while oper is None:
oper = raw_input("Enter operation [+, -, *, /]> ")
if oper[0] in "+-*/":
oper = oper[0]
else:
oper = None
#note -- I'm too lazy to look up the operator module, but the
#above could have used a dictionary look-up to assign the
#actual function to oper, and then the following if-block
#becomes a simple
# cumulative = oper(cumulative, item)
cumulative = items[0]
for item in items[1:]:
if oper == "+":
cumulative += item
elif oper == "-":
cumulative -= item
elif oper == "*":
cumulative *= item
elif oper == "/":
cumulative /= item
else:
print "Programming Error -- operator is invalid after passing
check"
print "Cumulative result is %s" % cumulative
-=-=-=-=-=-=-
C:\Users\Wulfraed\Documents>oper.py
How many values? 3
3
Enter item #1> 21
Enter item #2> 32
Enter item #3> 43
Enter operation [+, -, *, /]> +
Cumulative result is 96
How many values? 4
4
Enter item #1> 999
Enter item #2> 321
Enter item #3> 123
Enter item #4> 42
Enter operation [+, -, *, /]> -
Cumulative result is 513
How many values? 2
2
Enter item #1> 4737
Enter item #2> 121
Enter operation [+, -, *, /]> *
Cumulative result is 573177
How many values?
Invalid input, exiting...
C:\Users\Wulfraed\Documents>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Efficency help for a Calculator Program JonDoe297 <vignesh.harikrishna@gmail.com> - 2013-10-02 03:44 -0700
Re: Efficency help for a Calculator Program Chris Angelico <rosuav@gmail.com> - 2013-10-02 21:01 +1000
Re: Efficency help for a Calculator Program JonDoe297 <vignesh.harikrishna@gmail.com> - 2013-10-02 06:43 -0700
Re: Efficency help for a Calculator Program Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-02 19:47 -0400
Re: Efficency help for a Calculator Program Chris Angelico <rosuav@gmail.com> - 2013-10-03 10:25 +1000
Re: Efficency help for a Calculator Program Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-03 19:15 -0400
Re: Efficency help for a Calculator Program Chris Angelico <rosuav@gmail.com> - 2013-10-04 16:45 +1000
csiph-web