Some python newb help please?

L

lrwarren94

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 :)
 
L

lrwarren94

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."
 
J

John Strick

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 evenon 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
 
M

MRAB

On 12/11/2013 22:27, (e-mail address removed) wrote:> On Tuesday, November
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:
...
...
 
T

Tim Chase

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
 
L

lrwarren94

On 12/11/2013 22:27, (e-mail address removed) wrote:> On Tuesday, November


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
quitCommand = 0

print "Welcome to the World of Textcraft!"
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

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 :\
 
C

Chris Angelico

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
 
D

Dennis Lee Bieber

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}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top