0

If the user enters only one character, I want it to prompt for the name again; otherwise print the persons name in ASCII. Instead what is happening is it's printing the name depending on how many characters are in the name ?

nameAsk = raw_input("What is your name?")
for name in nameAsk:
  if (len(name)>=2):
    print(ord(name))
  else:
    raw_input(nameAsk)
3
Contributors
5
Replies
33
Views
2 Months
Discussion Span
Last Post by Gribouillis
1

You wrote raw_input(nameAsk) instead of raw_input('What is your name'). I would also use a while loop. The idiom while True means repeat indefinitely, so my loop runs until the break statement exits the loop. The advantage is that I don't have to write the raw_input() statement twice.

while True:
    name = raw_input('What is your name: ').strip()
    if len(name) >= 2:
        break

for c in name:
    print ord(c),

By the way, why are you learning with python 2 instead of python 3 ?

Edited by Gribouillis

0

Thanks, wasn't aware you could append a string operation; then again strip() is a string operation :)
I'm learning both; some program API I'll be using only use Python 2.

1

As long as you think the result of an expression is a string, you can append a string operation. For example the result of raw_input('name? ').strip() is a string, so I can append .capitalize(). The same applies to any data type.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.