Python Forum

Full Version: Sequence subtraction
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! I am making a game where you enter a path for a drone to fly in, and you must enter the movements right. In the case below, the correct way to get through would be the sequence sddddwx. However, the player can get a 'free-bee', and have any number of characters wrong, and still pass. The errors that will pass unnoticed will be defined using the variable error. How would I make a program that would not notice a number of wrong characters? Thank you.
The error is defined:

print"Newest upgrade purchased!"
error = error + 1
money = money - 1000000
print"You can make",error," mistakes."
The maze:

    print("""

|A----B|    From A drop Paratrooper 4 (v) at B
|______|        Time limit: None.""")
r1 = raw_input("Commands:")
1. Next time. Show the work. That you want help on.

It pretty simple.
Just loop over word.
Compare against correct word code.

Try it yourself then look at code.
What do you mean by

Quote:Just loop over word.
Loop over the word.
Then you compare it to your correct output.

for loop.
word = 'sdddwx'
for letter in word:
    print(letter)
for range loop
word = 'sdddwx'
for i in range(len(word)):
    print(word[i])
while loop
word = 'sdddwx'
length = len(word)
i = 0
while i < length:
    print(word[i])
    i += 1
How do I compare it against the correct word code?
1. Seems like you need to go through the beginner tutorials. Before making a game.
basic programming.
a. variables (integer, float, strings, etc)
b. logic and logic operators (if .. elif .. else) (==, !=. etc)
c. loops (for, while)
d. functions (def)
e. containers (tuple, list, dict, class)
f. keywords

If logic.
code = 'sddddwx'
word = 'sdddwwx'
if word[0] == code[0]:
    print(True)
example. Try mixing logic with a loop. Before looking at code.
Now, how do I get rid of errors if the string is only one letter, while using the code below:
code = raw_input("Code:")
errors = 0
word = 'sdddwwx'
if word[0] == code[0]:
    print("Good!")
else:
    errors = errors + 1

if word[1] == code[1]:
    print("Good!")
else:
    errors = errors + 1
Take a look at the Hide/Show in Windspar's last post.
To solve your problem. A few keywords and a loop with logic.

keyword min
print(min(5, 3))
keyword range.
print(list(range(10))
for i in range(10):
    print(i)
keyword len
word = 'sddwx'
print(len(word))
play with code.
try to solve it yourself.
then look at code in above post.
So, this is the final code:

errorlevel = 0
errors = 0
r1 = raw_input("Commands:")
word = 'sdddwwx'
for i in range(min(len(r1), len(word))):
    if r1[i] != word[i]:
        errors += 1
if errorlevel >= errors or errors == 0:
    print"Good!"
else:
    print errors
Thank you for your help.