-4

I've written the code below and need to fill out a variable table like this:

  • Variable name:
  • Type:
  • Validation:
  • Description:

for all variables. However, as I am really really new to python I don't know what the variables are, let alone their details.

I found this example:

  • Variable name: userName
  • Type: String
  • Validation: Must only contain characters a-z/A-Z.
  • Description: Will be used to store the user’s name.

But I don't know how to do it for the variables in my code. Please help if you can.

print("Please enter a sentence")
sentence=input ()
lowersen=(sentence.lower () )
print(lowersen)
splitlowersen=(lowesen.strip () )
splitlowersen = "".join(c for c in splitlowersen if c not in ('!','.',':','?',';'))
print("Enter word")
word=input()
lword=(word.lower () )
if lword in splitlowersen:
    print(lword, "is in sentence")
    for i, j in enumerate (splitlowersen) :
        if j==lword:
            print(""+lword+"","is in position", i+1)

if lword not in splitlowersen:
    print (lword, "is not in sentence")

print ("End")
berna1111
  • 1,811
  • 1
  • 18
  • 23
  • Maybe start [here](https://www.learnpython.org/en/Variables_and_Types). I don't understand what you mean by "Validation", as assigning a variable in python has no restriction on what that variable might hold - after assigning that variable one could test it, but there are no such tests in your example. – berna1111 Mar 13 '17 at 11:56

3 Answers3

0

i don't know Phython well but i think it is

Variable name: lword

Type: String

Validation: contains letters, numbers and special characters .

Description: Will be used to check if this word is in the sentence you wrote earlier

That would be for the variable lword

Dr3xler
  • 110
  • 4
0

By the way, you can write this:

print(“Please enter a sentence”)
sentence=input ()

as this:

sentence=input(“Please enter a sentence”)

Regarding your question, I will give you an example and continue by your own. Example: Variable name: sentence

Type: String

Validation: It may contain letters, numbers, special characters and white spaces.

Description: It is a complete sentence.

Ambitions
  • 2,369
  • 3
  • 13
  • 24
0

I changed your code a bit, hope this helps:

sentence = raw_input("Please enter a sentence: ")
# Variable name: `sentence`
# Type: string
# Validation: ?
# Description: the original inputted sentence.
lowersen = sentence.lower()
# Variable name: `lowersen`
# Type: string
# Validation: ?
# Description: the inputted sentence in lowercase only.
print(lowersen)
splitlowersen = lowersen.strip()
# Variable name: `splitlowersen`
# Type: string
# Validation: ?
# Description: lowersen with leading and trailing whitespace removed...
forb_symbs = ('!', '.', ':', '?', ';') # not originally in the example!
splitlowersen = "".join(c for c in splitlowersen if c not in forb_symbs)
# Description (cont.): ... and without the symbols in the list `forb_symbs`
splitlowersen = splitlowersen.split(' ') # not originally in the example!
# Variable name: `splitlowersen`
# Type: list
# Validation: ?
# Description: la list of words in the sentence.

word = raw_input("Enter one word: ")
# Variable name: `word`
# Type: string
# Validation: ?
# Description: the original inputted word.
lword = word.lower()
# Variable name: `lword`
# Type: string
# Validation: ?
# Description: the original inputted word in lowercase only.

if lword in splitlowersen:
    print('"' + lword + '" is in sentence!')
    for i, j in enumerate(splitlowersen):
        # Variable name: `i`
        # Type: int
        # Validation: ?
        # Description: index of the list currently being probbed 
        # Variable name: `j`
        # Type: string
        # Validation: ?
        # Description: word in the position `i` in the sentence
        if j == lword:
            print('"'+lword+'" is in position ' + str(i+1) + '.')
            break # not necessary to continue the iteration
else: # if lword not in splitlowersen:
    print ('"' + lword + '" is not in sentence!')
print("End")

One of the things you should notice with particular attention is the change in type of splitlowersen. There's no validation when assigning values to variables in python, unless you make your own (such as setter methods of properties)

Another thing, I'm using python 2.7, hence the use of raw_input instead of input. In 3.0 you must use input.

Community
  • 1
  • 1
berna1111
  • 1,811
  • 1
  • 18
  • 23