Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99172 > unrolled thread
| Started by | Dylan Riley <dylan.riley@hotmail.com> |
|---|---|
| First post | 2015-11-20 09:22 -0800 |
| Last post | 2015-11-20 18:05 -0500 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | Dylan Riley <dylan.riley@hotmail.com> |
|---|---|
| Date | 2015-11-20 09:22 -0800 |
| Subject | error help import random |
| Message-ID | <8b0ec311-ab7f-429d-877f-d205ad6008d9@googlegroups.com> |
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")
print("hope ypu enjoyed your fortune being told")
input("\nPress enter to exit")
many thanks in advance
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-11-21 04:30 +1100 |
| Message-ID | <mailman.11.1448040645.2291.python-list@python.org> |
| In reply to | #99172 |
On Sat, Nov 21, 2015 at 4:22 AM, Dylan Riley <dylan.riley@hotmail.com> 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 Actually, you could help us identify the error. What happens when you run it? Show us exactly how you invoke it and what the output is. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-11-20 18:57 +0100 |
| Message-ID | <mailman.13.1448042279.2291.python-list@python.org> |
| In reply to | #99172 |
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")
>
> print("hope ypu enjoyed your fortune being told")
> input("\nPress enter to exit")
>
>
> many thanks in advance
Make sure that you run your code with Python 3, not Python 2.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-11-20 11:10 -0700 |
| Message-ID | <mailman.14.1448043092.2291.python-list@python.org> |
| In reply to | #99172 |
On Fri, Nov 20, 2015 at 10:57 AM, Peter Otten <__peter__@web.de> wrote:
> Dylan Riley wrote:
>
>> input("\nPress enter to see your fortune")
>
> Make sure that you run your code with Python 3, not Python 2.
Or if you must use Python 2, use raw_input() instead of input().
>> fortune = random.randrange(6) + 1
Also, you have an off-by-one error here. randrange(6) will give you an
integer between 0 and 5, inclusive (a total of 6 possible values).
Adding 1 will then result in an integer between 1 and 6, inclusive,
but you don't have a case for a value of 6.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2015-11-20 15:15 -0500 |
| Message-ID | <mailman.21.1448050562.2291.python-list@python.org> |
| In reply to | #99172 |
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
[toc] | [prev] | [next] | [standalone]
| From | Rob Gaddi <rgaddi@technologyhighland.invalid> |
|---|---|
| Date | 2015-11-20 23:03 +0000 |
| Message-ID | <n2o8ru$m03$1@dont-email.me> |
| In reply to | #99186 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Dylan Riley <dylan.riley@hotmail.com> |
|---|---|
| Date | 2015-11-20 19:26 -0800 |
| Message-ID | <66756cd4-d3ef-42db-8471-8475ed540a27@googlegroups.com> |
| In reply to | #99189 |
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
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2015-11-20 18:05 -0500 |
| Message-ID | <mn9v4btss3epg7ijle8b8kg2nnqbkip4el@4ax.com> |
| In reply to | #99172 |
On Fri, 20 Nov 2015 09:22:10 -0800 (PST), Dylan Riley
<dylan.riley@hotmail.com> 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
>
A problem I used to have is that if you have a file called random.py
in the folder when you try to import random, it caused an error.
>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")
>
>print("hope ypu enjoyed your fortune being told")
>input("\nPress enter to exit")
>
>
>many thanks in advance
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web