Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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
为什么';t';如果';功能工作?python_Python_If Statement - Fatal编程技术网

为什么';t';如果';功能工作?python

为什么';t';如果';功能工作?python,python,if-statement,Python,If Statement,如果我尝试运行此代码,它会工作,但它会跳过“if”函数。。。请帮忙。谢谢 e必须是int def guessinggame (): d = input("Enter a random word: ") e = input("How many letters does the word has?: ") f = len(d) if( "e" == len(d)): print("You are right!") guessinggame() 您只处理

如果我尝试运行此代码,它会工作,但它会跳过“if”函数。。。请帮忙。谢谢

e
必须是int

def guessinggame ():
    d = input("Enter a random word: ")
    e = input("How many letters does the word has?: ")
    f = len(d)
    if( "e" == len(d)):
       print("You are right!")
guessinggame()

您只处理字符串,从不将任何内容转换为整数,而且(更重要的是)将字符串与引用名称混淆。对于整数,请使用
int()
强制转换:

def guessinggame(): 
    d = input("Enter a random word: ")
    e = input("How many letters does the word has?: ") 
    f = len(d) 
    if int(e) == len(d): 
        print("You are right!")
guessinggame()
对于字符串和引用名称,
'e'
只是字母“e”。省略引号,然后您将使用引用
e

e = int(input("How many letters does the word has?: "))

你需要
if(int(e)==len(d)):
假设
len(d)
是5,那么
的结果是什么。你认为
“e”
在那个测试中是什么?它与您引用的
d
有何区别?另外,如果这是Python3,变量
e
将包含一个字符串,而不是一个整数,请参见不要跳过if语句(它不是函数)。你刚刚写的条件非常非常不正确,所以它总是评估为false。欢迎使用so!记住,如果你的问题遇到类似的问题,你的问题应该用一种对其他人也有用的格式来表达。按原样阅读你的问题,它不是那样格式化的。试着包括解释器打印的错误,并解释您尝试了什么,以及您之前为理解错误做了哪些研究。
if e == len(d):