Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59237 > unrolled thread
| Started by | lrwarren94@gmail.com |
|---|---|
| First post | 2013-11-12 14:14 -0800 |
| Last post | 2013-11-12 23:05 +0000 |
| Articles | 12 — 8 participants |
Back to article view | Back to comp.lang.python
Some python newb help please? lrwarren94@gmail.com - 2013-11-12 14:14 -0800
Re: Some python newb help please? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-12 22:21 +0000
Re: Some python newb help please? lrwarren94@gmail.com - 2013-11-12 14:27 -0800
Re: Some python newb help please? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-12 22:54 +0000
Re: Some python newb help please? MRAB <python@mrabarnett.plus.com> - 2013-11-12 22:56 +0000
Re: Some python newb help please? lrwarren94@gmail.com - 2013-11-12 15:04 -0800
Re: Some python newb help please? Chris Angelico <rosuav@gmail.com> - 2013-11-13 10:19 +1100
Re: Some python newb help please? Tim Chase <python.list@tim.thechases.com> - 2013-11-12 16:59 -0600
Re: Some python newb help please? Chris Angelico <rosuav@gmail.com> - 2013-11-13 09:58 +1100
Re: Some python newb help please? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-12 20:49 -0500
Re: Some python newb help please? John Strick <jstrickler@gmail.com> - 2013-11-12 14:49 -0800
Re: Some python newb help please? Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-12 23:05 +0000
| From | lrwarren94@gmail.com |
|---|---|
| Date | 2013-11-12 14:14 -0800 |
| Subject | Some python newb help please? |
| Message-ID | <f9bcd68d-ac00-421a-ade5-5f5690861d25@googlegroups.com> |
So I'm trying to write a program for a problem in class, and something strange is happening that I can't figure out why is happening. I was wondering if you guys could help me fix it? http://pastebin.com/6QZTvx6Z Basically, 1 and 2 work just fine as inputs, but whenever I input 3 or 4, idle just doesn't do anything. Does anyone know why that is? any suggestions on how to fix? Any help is much appreciated :)
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-12 22:21 +0000 |
| Message-ID | <mailman.2490.1384294935.18130.python-list@python.org> |
| In reply to | #59237 |
On 12/11/2013 22:14, lrwarren94@gmail.com wrote: > So I'm trying to write a program for a problem in class, and something strange is happening that I can't figure out why is happening. I was wondering if you guys could help me fix it? > > http://pastebin.com/6QZTvx6Z > > Basically, 1 and 2 work just fine as inputs, but whenever I input 3 or 4, idle just doesn't do anything. Does anyone know why that is? any suggestions on how to fix? Any help is much appreciated :) > Please put your code inline so we can see it, if it's too long see this http://sscce.org/ for advice. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | lrwarren94@gmail.com |
|---|---|
| Date | 2013-11-12 14:27 -0800 |
| Message-ID | <22894a10-1ffd-4671-94f4-962dfb56e4fd@googlegroups.com> |
| In reply to | #59238 |
On Tuesday, November 12, 2013 4:21:58 PM UTC-6, Mark Lawrence wrote:
> On 12/11/2013 22:14, lr....@gmail.com wrote:
>
> > So I'm trying to write a program for a problem in class, and something strange is happening that I can't figure out why is happening. I was wondering if you guys could help me fix it?
>
> >
>
> > http://pastebin.com/6QZTvx6Z
>
> >
>
> > Basically, 1 and 2 work just fine as inputs, but whenever I input 3 or 4, idle just doesn't do anything. Does anyone know why that is? any suggestions on how to fix? Any help is much appreciated :)
>
> >
>
>
>
> Please put your code inline so we can see it, if it's too long see this
>
> http://sscce.org/ for advice.
>
>
>
> --
>
> Python is the second best programming language in the world.
>
> But the best has yet to be invented. Christian Tismer
>
>
>
> Mark Lawrence
I'm not quite sure what you mean by that. it was on that pastebin link. I'll post it again here though. it's no longer than half a page.
x = 0
y = 0
quitCommand = 0
print "Welcome to the World of Textcraft!"
print "----------------------------------"
print ""
while quitCommand != int(5):
print "You are currently at (" + str(x) + ", " + str(y) + ")"
print "Enter a command (1 = North, 2 = East, 3 = South, 4 = West, 5 = Exit):"
if int(raw_input()) == 1:
print "Moving north"
y = y + 1
elif int(raw_input()) == 2:
print "Moving east"
x = x + 1
elif int(raw_input()) == 3:
print "Moving south"
y = y - 1
elif int(raw_input()) == 4:
print "Moving west"
x = x - 1
elif int(raw_input()) == 5:
print "Dost thou leave so soon? Fare thee well!"
quitCommand = 5
else:
print "I find your lack of reading comprehension skills disturbing."
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-12 22:54 +0000 |
| Message-ID | <mailman.2494.1384296897.18130.python-list@python.org> |
| In reply to | #59239 |
First thing would you please read and action this
https://wiki.python.org/moin/GoogleGroupsPython so we don't have to read
double spaced google crap, thanks.
On 12/11/2013 22:27, lrwarren94@gmail.com wrote:
> On Tuesday, November 12, 2013 4:21:58 PM UTC-6, Mark Lawrence wrote:
>> On 12/11/2013 22:14, lr....@gmail.com wrote:
>>
>>> So I'm trying to write a program for a problem in class, and something strange is happening that I can't figure out why is happening. I was wondering if you guys could help me fix it?
>>
>>>
>>
>>> http://pastebin.com/6QZTvx6Z
>>
>>>
>>
>>> Basically, 1 and 2 work just fine as inputs, but whenever I input 3 or 4, idle just doesn't do anything. Does anyone know why that is? any suggestions on how to fix? Any help is much appreciated :)
>>
>>>
>>
>>
>>
>> Please put your code inline so we can see it, if it's too long see this
>>
>> http://sscce.org/ for advice.
>>
>>
>>
>> --
>>
>> Python is the second best programming language in the world.
>>
>> But the best has yet to be invented. Christian Tismer
>>
>>
>>
>> Mark Lawrence
>
> I'm not quite sure what you mean by that. it was on that pastebin link. I'll post it again here though. it's no longer than half a page.
>
> x = 0
> y = 0
> quitCommand = 0
>
> print "Welcome to the World of Textcraft!"
> print "----------------------------------"
> print ""
You don't need the double quotes in the line above, the print statement
on its own will output a newline.
>
> while quitCommand != int(5):
> print "You are currently at (" + str(x) + ", " + str(y) + ")"
> print "Enter a command (1 = North, 2 = East, 3 = South, 4 = West, 5 = Exit):"
> if int(raw_input()) == 1:
> print "Moving north"
> y = y + 1
> elif int(raw_input()) == 2:
> print "Moving east"
> x = x + 1
> elif int(raw_input()) == 3:
> print "Moving south"
> y = y - 1
> elif int(raw_input()) == 4:
> print "Moving west"
> x = x - 1
> elif int(raw_input()) == 5:
> print "Dost thou leave so soon? Fare thee well!"
> quitCommand = 5
> else:
> print "I find your lack of reading comprehension skills disturbing."
>
>
You're asking for input in every comparison. Change this to request the
input once, store it and then compare it. If you rename quitCommand to
command, you'd have
command = int(raw_input())
if command == 1:
etc.
Enjoy :)
--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-11-12 22:56 +0000 |
| Message-ID | <mailman.2495.1384296995.18130.python-list@python.org> |
| In reply to | #59239 |
On 12/11/2013 22:27, lrwarren94@gmail.com wrote:> On Tuesday, November
12, 2013 4:21:58 PM UTC-6, Mark Lawrence wrote:
>> On 12/11/2013 22:14, lr....@gmail.com wrote:
>>
>> > So I'm trying to write a program for a problem in class, and
something strange is happening that I can't figure out why is happening.
I was wondering if you guys could help me fix it?
>> >
[snip]
>
> x = 0
> y = 0
> quitCommand = 0
>
> print "Welcome to the World of Textcraft!"
> print "----------------------------------"
> print ""
You can simplify that to:
print
>
> while quitCommand != int(5):
5 is already an int, so int(5) == 5.
> print "You are currently at (" + str(x) + ", " + str(y) + ")"
> print "Enter a command (1 = North, 2 = East, 3 = South, 4 =
West, 5 = Exit):"
> if int(raw_input()) == 1:
You're asking the user to enter something and then checking whether its
int value is 1.
> print "Moving north"
> y = y + 1
> elif int(raw_input()) == 2:
Now you're asking the user to enter something _again_ and then checking
whether its int value is 2.
In other words, in order for it to print "Moving east" the following
steps must occur:
1. Ask the user to enter something.
2. Check whether it's 1. It isn't. (Previous condition)
3. Ask the user to enter something.
4. Check whether it's 2. (This condition)
> print "Moving east"
> x = x + 1
> elif int(raw_input()) == 3:
Similar remarks to above, but longer.
> print "Moving south"
> y = y - 1
> elif int(raw_input()) == 4:
Similar remarks to above, but longer again.
> print "Moving west"
> x = x - 1
> elif int(raw_input()) == 5:
Similar remarks to above, but longer again.
> print "Dost thou leave so soon? Fare thee well!"
> quitCommand = 5
> else:
> print "I find your lack of reading comprehension skills
disturbing."
>
The fix is simple. Ask once:
answer = int(raw_input())
if answer == 1:
...
elif answer == 2:
...
...
[toc] | [prev] | [next] | [standalone]
| From | lrwarren94@gmail.com |
|---|---|
| Date | 2013-11-12 15:04 -0800 |
| Message-ID | <3a858f41-2c88-4705-9649-969eca758cbf@googlegroups.com> |
| In reply to | #59246 |
On Tuesday, November 12, 2013 4:56:35 PM UTC-6, MRAB wrote:
> On 12/11/2013 22:27, l...@gmail.com wrote:> On Tuesday, November
>
> 12, 2013 4:21:58 PM UTC-6, Mark Lawrence wrote:
>
> >> On 12/11/2013 22:14, lr....@gmail.com wrote:
>
> >>
>
> >> > So I'm trying to write a program for a problem in class, and
>
> something strange is happening that I can't figure out why is happening.
>
> I was wondering if you guys could help me fix it?
>
> >> >
>
> [snip]
>
> >
>
> > x = 0
>
> > y = 0
>
> > quitCommand = 0
>
> >
>
> > print "Welcome to the World of Textcraft!"
>
> > print "----------------------------------"
>
> > print ""
>
>
>
> You can simplify that to:
>
>
>
> print
>
>
>
> >
>
> > while quitCommand != int(5):
>
>
>
> 5 is already an int, so int(5) == 5.
>
>
>
> > print "You are currently at (" + str(x) + ", " + str(y) + ")"
>
> > print "Enter a command (1 = North, 2 = East, 3 = South, 4 =
>
> West, 5 = Exit):"
>
> > if int(raw_input()) == 1:
>
>
>
> You're asking the user to enter something and then checking whether its
>
> int value is 1.
>
>
>
> > print "Moving north"
>
> > y = y + 1
>
> > elif int(raw_input()) == 2:
>
>
>
> Now you're asking the user to enter something _again_ and then checking
>
> whether its int value is 2.
>
>
>
> In other words, in order for it to print "Moving east" the following
>
> steps must occur:
>
>
>
> 1. Ask the user to enter something.
>
>
>
> 2. Check whether it's 1. It isn't. (Previous condition)
>
>
>
> 3. Ask the user to enter something.
>
>
>
> 4. Check whether it's 2. (This condition)
>
>
>
> > print "Moving east"
>
> > x = x + 1
>
> > elif int(raw_input()) == 3:
>
>
>
> Similar remarks to above, but longer.
>
>
>
> > print "Moving south"
>
> > y = y - 1
>
> > elif int(raw_input()) == 4:
>
>
>
> Similar remarks to above, but longer again.
>
>
>
> > print "Moving west"
>
> > x = x - 1
>
> > elif int(raw_input()) == 5:
>
>
>
> Similar remarks to above, but longer again.
>
>
>
> > print "Dost thou leave so soon? Fare thee well!"
>
> > quitCommand = 5
>
> > else:
>
> > print "I find your lack of reading comprehension skills
>
> disturbing."
>
> >
>
>
>
> The fix is simple. Ask once:
>
>
>
> answer = int(raw_input())
>
> if answer == 1:
>
> ...
>
> elif answer == 2:
>
> ...
>
> ...
Thanks a lot! I'll try this out!
Sorry to everyone else whose eyes I made bleed. I've never used a newsgroup before...still not really sure what they are. Found this through a google search :\
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-13 10:19 +1100 |
| Message-ID | <mailman.2505.1384298395.18130.python-list@python.org> |
| In reply to | #59250 |
On Wed, Nov 13, 2013 at 10:04 AM, <lrwarren94@gmail.com> wrote: > Thanks a lot! I'll try this out! > Sorry to everyone else whose eyes I made bleed. I've never used a newsgroup before...still not really sure what they are. Found this through a google search :\ There's an easy fix. Go to this page: https://mail.python.org/mailman/listinfo/python-list You can then subscribe to the mailing list using whatever email address you're comfortable with. I use Gmail, which works fairly well but has its own issues; I've glanced at Evolution (a Linux mail client) and its way of handling threads, and it seems to do a good job. Plenty of options. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-11-12 16:59 -0600 |
| Message-ID | <mailman.2496.1384297075.18130.python-list@python.org> |
| In reply to | #59239 |
On 2013-11-12 14:27, lrwarren94@gmail.com wrote: > if int(raw_input()) == 1: > print "Moving north" > y = y + 1 > elif int(raw_input()) == 2: > print "Moving east" > x = x + 1 > elif int(raw_input()) == 3: > print "Moving south" > y = y - 1 > elif int(raw_input()) == 4: > print "Moving west" > x = x - 1 > elif int(raw_input()) == 5: > print "Dost thou leave so soon? Fare thee well!" > quitCommand = 5 > else: > print "I find your lack of reading comprehension skills > disturbing." Note that you're asking for input with each comparison. Best to get the input once and store it in a variable before the "if" statement and then do the comparisons against the same value. -tkc
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-13 09:58 +1100 |
| Message-ID | <mailman.2497.1384297128.18130.python-list@python.org> |
| In reply to | #59239 |
On Wed, Nov 13, 2013 at 9:27 AM, <lrwarren94@gmail.com> wrote: > I'm not quite sure what you mean by that. it was on that pastebin link. I'll post it again here though. it's no longer than half a page. Inline means what you did in this post. Out-of-line means providing us with a link to where the code is. This forum isn't just a Google Groups web-based discussion group; it's primarily a newsgroup (comp.lang.python) and an email address (python-list@python.org). Both of those can be read by people who don't have internet access, so putting your code in an http link might prevent them from reading it. Also, the group/list gets archived all over the place, and there's no guarantee that pastebin will still be around a hundred years from now. It's an unnecessary dependency, which inline code doesn't have. That's why posting right here is the safe option :) Two general comments. Firstly, you're using Python 2 here. Is there a reason for that? If you possibly can, switch to Python 3. All sorts of things have been improved, and the gap is only going to widen - there won't be a Python 2.8, and subsequent 2.7.x releases are bugfixes and security patches only. All the new shinies are in 3.x. And secondly, please PLEASE avoid Google Groups; your quoted text is ugly and annoying, and your own text isn't wrapped. Check this out, and preferably, find an alternative means of posting: https://wiki.python.org/moin/GoogleGroupsPython ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-11-12 20:49 -0500 |
| Message-ID | <mailman.2521.1384307390.18130.python-list@python.org> |
| In reply to | #59239 |
On Tue, 12 Nov 2013 14:27:37 -0800 (PST), lrwarren94@gmail.com declaimed
the following:
>
>I'm not quite sure what you mean by that. it was on that pastebin link. I'll post it again here though. it's no longer than half a page.
The preference here is that if you ask a question about code, you
provide the code IN the message -- you don't send us off to some site that
may or may not retain the code down the road (whereas the question posted
here may be archived forever)
>
>x = 0
>y = 0
>quitCommand = 0
>
>print "Welcome to the World of Textcraft!"
>print "----------------------------------"
>print ""
>
>while quitCommand != int(5):
5 IS an int, so why coerce it to itself.
> print "You are currently at (" + str(x) + ", " + str(y) + ")"
> print "Enter a command (1 = North, 2 = East, 3 = South, 4 = West, 5 = Exit):"
> if int(raw_input()) == 1:
> print "Moving north"
> y = y + 1
> elif int(raw_input()) == 2:
> print "Moving east"
> x = x + 1
> elif int(raw_input()) == 3:
> print "Moving south"
> y = y - 1
> elif int(raw_input()) == 4:
> print "Moving west"
> x = x - 1
> elif int(raw_input()) == 5:
> print "Dost thou leave so soon? Fare thee well!"
> quitCommand = 5
> else:
> print "I find your lack of reading comprehension skills disturbing."
>
Note that you ask for NEW input from the user on each if/elif. You want
to read ONE value from the user, and then compare THAT value on each
if/elif line.
HINT: raw_input() can display a prompt string... so
command = int(raw_input(
"Enter a command "
"(1 = North, 2 = East, 3 = South, 4 = West, 5 = Exit): "))
{Note: I took advantage of Python's ability to concatenate adjacent strings
-- that way the line doesn't wrap at odd locations in UseNet}
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | John Strick <jstrickler@gmail.com> |
|---|---|
| Date | 2013-11-12 14:49 -0800 |
| Message-ID | <844919dc-5c22-41ac-8bd6-4ef11795e7f3@googlegroups.com> |
| In reply to | #59237 |
Welcome to the world of Python programming! I'm glad you're learning this great language. As to your bug, think about this: in each if or elif statement, you're reading the user input again, so if user input is NOT equal to 1 in the first place, it reads input again. Try to step through your code mentally, or even on paper, and track *exactly* what's happening. You could also add some print statements to see where you are. Good luck with your coding. Consider reading user input only once, and then checking it against the values 1,2,3,4, and 5. As an aside, "int(5)" is from the department of redundancy department. ;-) --John Strickler On Tuesday, November 12, 2013 5:14:42 PM UTC-5, lrwar...@gmail.com wrote: > So I'm trying to write a program for a problem in class, and something strange is happening that I can't figure out why is happening. I was wondering if you guys could help me fix it? > > > > http://pastebin.com/6QZTvx6Z > > > > Basically, 1 and 2 work just fine as inputs, but whenever I input 3 or 4, idle just doesn't do anything. Does anyone know why that is? any suggestions on how to fix? Any help is much appreciated :)
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-11-12 23:05 +0000 |
| Message-ID | <l5uc82$olu$2@dont-email.me> |
| In reply to | #59237 |
On Tue, 12 Nov 2013 14:14:42 -0800, lrwarren94 wrote: > http://pastebin.com/6QZTvx6Z Work through your code very very carefully. You're doing something in each if branch that you probably only want to do once in each execution of the while loop. If you can't figure it out, I'll post a corrected version next week. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web