这段代码中有哪些变量?

时间:2017-03-13 11:47:11

标签: python python-2.7 python-3.x variables

我已经编写了下面的代码,需要填写这样的变量表:

  
      
  • 变量名称:
  •   
  • 类型:
  •   
  • 验证:
  •   
  • 说明
  •   

所有变量。但是,因为我对python真的很陌生,所以我不知道变量是什么,更不用说它们的细节了。

我找到了这个例子:

  
      
  • 变量名称:userName
  •   
  • 输入:String
  •   
  • 验证:Must only contain characters a-z/A-Z.
  •   
  • 说明:Will be used to store the user’s name.
  •   

但我不知道如何为我的代码中的变量做这件事。如果可以,请帮忙。

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")

3 个答案:

答案 0 :(得分:0)

我不太了解Phython,但我认为它是

变量名称:lword

类型:字符串

验证:包含字母,数字和特殊字符。

描述:将用于检查这个词是否在你之前写的句子中

那将是变量lword

答案 1 :(得分:0)

顺便说一下,你可以这样写:

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

这样:

sentence=input(“Please enter a sentence”)

关于你的问题,我会举个例子并继续你自己的问题。 例: 变量名称:句子

类型:字符串

验证:它可能包含字母,数字,特殊字符和空格。

描述:这是一个完整的句子。

答案 2 :(得分:0)

我改变了你的代码,希望这会有所帮助:

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")

您应特别注意的一件事是splitlowersen类型的变化。在为python中的变量赋值时,没有验证,除非你自己创建(例如setter methods of properties

另一件事,我使用python 2.7,因此使用raw_input instead of input。在3.0中,您必须使用input