Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99192

Re: error help import random

X-Received by 10.50.142.66 with SMTP id ru2mr4590427igb.4.1448076395014; Fri, 20 Nov 2015 19:26:35 -0800 (PST)
X-Received by 10.50.2.6 with SMTP id 6mr145086igq.5.1448076394983; Fri, 20 Nov 2015 19:26:34 -0800 (PST)
Path csiph.com!au2pb.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!news.glorb.com!i2no5588799igv.0!news-out.google.com!l1ni4049igd.0!nntp.google.com!i2no5588796igv.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Fri, 20 Nov 2015 19:26:34 -0800 (PST)
In-Reply-To <n2o8ru$m03$1@dont-email.me>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=188.29.164.157; posting-account=7Stc7AoAAABahSR6XLhqPaIN-s0qSeOb
NNTP-Posting-Host 188.29.164.157
References <8b0ec311-ab7f-429d-877f-d205ad6008d9@googlegroups.com> <mailman.21.1448050562.2291.python-list@python.org> <n2o8ru$m03$1@dont-email.me>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <66756cd4-d3ef-42db-8471-8475ed540a27@googlegroups.com> (permalink)
Subject Re: error help import random
From Dylan Riley <dylan.riley@hotmail.com>
Injection-Date Sat, 21 Nov 2015 03:26:35 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:99192

Show key headers only | View raw


On Friday, November 20, 2015 at 11:06:05 PM UTC, Rob Gaddi wrote:
> On Fri, 20 Nov 2015 15:15:42 -0500, Terry Reedy wrote:
> 
> > On 11/20/2015 12:22 PM, Dylan Riley wrote:
> >> This is my fortune cookie program i wrote in python.
> >> the problem is it will not run past the first line of input.
> >> could someone please identify the error and explain to me why.
> >> here is the code:
> >>
> >> #the program silulates a fortune cookie #the program should display one
> >> of five unique fortunes, at randon, each time its run
> >>
> >> import random
> >>
> >> print("  \\ \\ \\ \\ DYLANS FORTUNE COOKIE \\ \\ \\ ")
> >> print("\n\n\tGood things come to those who wait")
> >> input("\nPress enter to see your fortune")
> >>
> >> fortune = random.randrange(6) + 1 print(fortune)
> >> if fortune == 1:
> >>      print("happy")
> >> elif fortune == 2:
> >>      print("horny")
> >> elif fortune == 3:
> >>      print("messy")
> >> elif fortune == 4:
> >>      print("sad")
> >> elif fortune == 5:
> >>      print("lool")
> > 
> > Use a dict instead of if-elif.
> > 
> > i = random.randrange(6)
> > fortunes = {0:'happy', 1:'horny', 2:'messy',
> >              3:'sad', 4:'lool', 5:'buggy'}
> > print(i, fortunes[i])
> > 
> 
> Or a list/tuple, which saves in typing, memory, and access time.
> 
>   fortunes = ('happy', 'horny', 'messy', 'sad', 'lool', 'buggy')
>   i = random.randrange(len(fortunes))
>   print(i, fortunes[i])
> 
> Or to be lazier still, just use random.choice
> 
>   fortunes = ('happy', 'horny', 'messy', 'sad', 'lool', 'buggy')
>   print(random.choice(fortunes))
> 
> None of which actually addresses the OP's issue with input(), but it's 
> nice to get the back half clean as well.
> 
> -- 
> Rob Gaddi, Highland Technology -- www.highlandtechnology.com
> Email address domain is currently out of order.  See above to fix.

very nice i like the way you code. i managed to solve the problem it was because i wasnt using raw_input however i updated to python 3 so the original now works. many thanks anyway

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

error help import random Dylan Riley <dylan.riley@hotmail.com> - 2015-11-20 09:22 -0800
  Re: error help import random Chris Angelico <rosuav@gmail.com> - 2015-11-21 04:30 +1100
  Re: error help import random Peter Otten <__peter__@web.de> - 2015-11-20 18:57 +0100
  Re: error help import random Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-20 11:10 -0700
  Re: error help import random Terry Reedy <tjreedy@udel.edu> - 2015-11-20 15:15 -0500
    Re: error help import random Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-11-20 23:03 +0000
      Re: error help import random Dylan Riley <dylan.riley@hotmail.com> - 2015-11-20 19:26 -0800
  Re: error help import random Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-20 18:05 -0500

csiph-web