cars cars - 1 year ago 60
Python Question

How to gather information from user input and apply it elsewhere

Hi I am new to programming and I am trying to write a code that will gather information from the input and determine if it is a valid alphabet.

This is my code so far

words = []
word = input('Character: ')
while word:
if word not in words:
words.append(word)
word = input('Character: ')
print(''.join(words),'is a a valid alphabetical string.')

suppose I choose three letters then the output of my code then pressed enter on the fourth,
the code will be:

Character:a
Character:b
Character:c
Character:
abc is a valid alphabetical string.

I want to add to this code so that when I type in a character that is not
from the alphabet the code will do something like this.

Character:a
Character:b
Character:c
Character:4
4 is not in the alphabet.


This is how I want my program to work

enter image description here

enter image description here

Answer Source

Use str.isalpha() It is gives only true if all characters in the string are letters.

Example:

>>> 'test'.isalpha()
True
>>> 'test44'.isalpha()
False
>>> 'test test'.isalpha()
False

In your code:

words = []
word = input('Character: ')
while word:
 if word.isalpha() and word not in words:
   words.append(word)
 word = input('Character: ')
print(words,'is a a valid alphabetical string.')