Some python newb help please?

MRAB python at mrabarnett.plus.com
Tue Nov 12 23:56:35 CET 2013


On 12/11/2013 22:27, lrwarren94 at gmail.com wrote:> On Tuesday, November 
12, 2013 4:21:58 PM UTC-6, Mark Lawrence wrote:
 >> On 12/11/2013 22:14, lr.... at 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:
          ...
      ...



More information about the Python-list mailing list