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


Groups > comp.lang.python > #99186

Re: error help import random

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: error help import random
Date Fri, 20 Nov 2015 15:15:42 -0500
Lines 42
Message-ID <mailman.21.1448050562.2291.python-list@python.org> (permalink)
References <8b0ec311-ab7f-429d-877f-d205ad6008d9@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de XsuSkPpHkOhkZ/ZFCFLW/QT+urGx359+vL5xwaiCD9KA==
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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'subject:help': 0.07; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python.': 0.11; 'jan': 0.11; 'subject:error': 0.11; 'input.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'subject:import': 0.16; 'subject:random': 0.16; 'wrote:': 0.16; 'why.': 0.18; 'wrote': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'cookie': 0.29; 'code:': 0.29; 'random': 0.29; 'run': 0.33; 'problem': 0.33; 'could': 0.35; 'should': 0.36; 'instead': 0.36; 'received:71': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'display': 0.37; 'received:org': 0.37; 'things': 0.38; 'someone': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'identify': 0.61; 'fortune': 0.66; 'here': 0.66; 'fortunes': 0.84; 'riley': 0.84; 'received:fios.verizon.net': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host pool-71-185-227-36.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0
In-Reply-To <8b0ec311-ab7f-429d-877f-d205ad6008d9@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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>
Xref csiph.com comp.lang.python:99186

Show key headers only | View raw


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])

> print("hope ypu enjoyed your fortune being told")
> input("\nPress enter to exit")


-- 
Terry Jan Reedy

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