Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这段代码中的变量是什么?_Python_Python 2.7_Python 3.x_Variables - Fatal编程技术网

Python 这段代码中的变量是什么?

Python 这段代码中的变量是什么?,python,python-2.7,python-3.x,variables,Python,Python 2.7,Python 3.x,Variables,我已经编写了下面的代码,需要填写如下的变量表: 变量名: 类型: 验证: 说明: 对于所有变量。然而,由于我对python非常陌生,我不知道变量是什么,更不用说它们的细节了 我发现这个例子: 变量名:userName 类型:String 验证:只能包含a-z/a-z字符。 描述:将用于存储用户名。 但是我不知道如何处理代码中的变量。如果可以的话,请帮忙 print("Please enter a sentence") sentence=input () lowersen=(sentenc

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

  • 变量名:
  • 类型:
  • 验证:
  • 说明:
对于所有变量。然而,由于我对python非常陌生,我不知道变量是什么,更不用说它们的细节了

我发现这个例子:

  • 变量名:
    userName
  • 类型:
    String
  • 验证:
    只能包含a-z/a-z字符。
  • 描述:
    将用于存储用户名。
但是我不知道如何处理代码中的变量。如果可以的话,请帮忙

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

我不太了解Phython,但我想是的

变量名称:lword

类型:字符串

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

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


这是变量lword的情况顺便说一下,您可以这样写:

print(“Please enter a sentence”)
sentence=input ()
因此:

sentence=input(“Please enter a sentence”)
关于你的问题,我会给你举个例子,然后你自己继续。 例子: 变量名:句子

类型:字符串

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


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

我稍微更改了您的代码,希望这对您有所帮助:

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中为变量赋值时没有验证,除非您自己创建(例如)


另外,我使用的是python 2.7,因此使用了。在3.0中,您必须使用
输入

句子、lowersen、splitlowersen、word、i和j是我可以看到的其他变量,因此farMaybe开始。我不明白你所说的“验证”是什么意思,因为在python中分配一个变量对该变量可能包含的内容没有限制——分配该变量后,可以对其进行测试,但在你的示例中没有这样的测试。