Conversion With 2 Languages Help
Author |
Message |
Guest
|
Posted: Tue May 30, 2006 7:26 pm Post subject: Conversion With 2 Languages Help |
|
|
Ok I have some Turing code which I wish to convert to Python. These two codes look exactly the same, but Python wont spit out my choice and gives me an error. Please help:
TURING:
code: |
% Drew Martell
%
% This will be my second program.
var firstName, lastName : string
put "Please enter your first name: " ..
get firstName
put "Please enter your last name: " ..
get lastName
put "Hello ", lastName, ' ', firstName (1)
var armour : array 1 .. 5 of string := init ("sword", "shield", "helmet", "chain mail", "plate leggings")
put "Here is some armour: " ..
for i : 1 .. 5
put armour (i), ' ' ..
end for
put ' '
loop
put "What armour do you wish to equip? " ..
var choice : string
get choice : *
for i : 1 .. 5
if (choice = armour (i)) then
put armour (i)
end if
end for
end loop
|
PYTHON:
code: |
# Drew Martell
#
# This will be my second program.
firstName = raw_input ("Please enter your first name: ")
lastName = raw_input ("Please enter your last name: ")
print "Hello "+lastName+", "+firstName[:1]
armour = ['sword','shield','helmet','chain mail','plate leggings']
print "Here is some armour: ", armour
while (1>0):
choice = raw_input ("What armour do you wish to equip? ")
for i in range(4):
if (choice==armour(i)):
print "You equip the", armour[i]
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Tue May 30, 2006 9:33 pm Post subject: (No subject) |
|
|
You may wish to make your code take adavantage of Python a bit more.
code: | first_name = raw_input("Please enter your first name: ")
last_name = raw_input("Please enter your last name: ")
print "Hello %s, %s" % (last_name, first_name[:1])
armour = ['sword', 'shield', 'helmet', 'chain mail', 'plate leggings']
print "Here is some armour: %s" % armour
while True:
choice = raw_input("What armour do you wish to equip? ")
for a in armour:
if choice == a:
print "You equip the %s" % a |
|
|
|
|
|
|
Guest
|
Posted: Wed May 31, 2006 11:03 pm Post subject: RE: Python |
|
|
Coolz. I like Python. So the percentages corrispond with whatever is in the brackets? Since I'm used to Turing, I'd think that whatever is displayed in the quotes either displays, indents, or moves down a line.
You always have %s, is that what you have to use? Or is it just recommended?
Also, how would I make it so the program recognizes an item as been entered, and then add another item. So I have this:
code: |
first_name = raw_input("Please enter your first name: ")
last_name = raw_input("Please enter your last name: ")
print "Hello %s, %s" % (last_name, first_name[:1])
armour = ['sword', 'shield', 'helmet', 'chain mail', 'plate leggings']
print "Here is some armour: %s" % armour
while True:
choice = raw_input("What armour do you wish to equip? ")
for a in armour:
if choice == a:
print "You equip the %s" % a
else:
print "(empty)"
|
I want it to check if a slot is open with (empty) in it, and replace it with the entered item, eg: "You equip the sword, a(1) = sword." "You equip the shield, a(2) = shield". Instead of (empty), display what is equipped =)
And one last question, your "While True", does that just loop it forever, until it somehow returns false? I try to use the Python Docs, but I get a "Page is Not Displayed" error, or whatever. I do have IE7 Beta, but when I had IE6 it did the same.[/code] |
|
|
|
|
|
wtd
|
Posted: Thu Jun 01, 2006 12:53 am Post subject: (No subject) |
|
|
Well, something like "Hello %s" is a format string. The "%s" is a format specifier. It says that the value inserted here should be a string. There are other specifiers for different types of values.
Now, we can use % in this case to substitute values into the format string to build a new string. Let's say:
code: | "Hello %s" % "world" |
And we get:
The parentheses I'm using when I have more than one value to deal with.
The problem is, the general form is:
format-string % value
In order to use multiple values, we have to group them into a single value. In many cases, the easiest way to do that is to construct a tuple.
code: | "Hello %s %s" % (first_name, last_name) |
As for "while True", well, "while" loops until the expression that follow evaluate to False. Since True is always True, then yes, it loops forever. Unless of course something inside the loop causes it to exit. |
|
|
|
|
|
|
|