0

I am new to python (coming from R) and trying to practice for loops, so I made up this challenge to have the computer guess the name string. Unfortunately I have stumped myself. Can anyone offer assistance?

## Guess a name

name = 'Kate'
char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRZTUVWXYZ'

for k in range(len(name)):
 for i in range(len(char)):
  if name[k] == char[i]:
     print('Your name is ' + char[i])

This gives the following output:

Your name is K
Your name is a
Your name is t
Your name is e

But I am looking for:

Your name is Kate

2 Answers 2

2

the print() call is called every time a character is guessed correctly.
to fix this, I would move the print("Your name is"...) function outside of the for loops. Python also has a way of changing how print() ends its lines.

...
print("Your name is ", end="")
for k in range(len(name)):
 for i in range(len(char)):
  if name[k] == char[i]:
     print(char[i], end="")
Sign up to request clarification or add additional context in comments.

Comments

1

Try to take a new empty string variable, and add it when the condition is satisfied and then after everything is done, print the variable outside the loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.