Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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 if语句中的输入未按预期工作_Python_Python 3.x_If Statement_Input - Fatal编程技术网

Python if语句中的输入未按预期工作

Python if语句中的输入未按预期工作,python,python-3.x,if-statement,input,Python,Python 3.x,If Statement,Input,if语句中的输入无效。我使用的是Python3,问题在于定义函数中的第一个if语句 import random random1 = random.randint(1, 11) random2 = random.randint(1, 11) correct = random1 * random2 list_of_correct = [] list_of_wrong = [] def ex(): proceed = input("Proceed?: ") if proceed.

if语句中的输入无效。我使用的是Python3,问题在于定义函数中的第一个if语句

import random

random1 = random.randint(1, 11)
random2 = random.randint(1, 11)
correct = random1 * random2
list_of_correct = []
list_of_wrong = []


def ex():
    proceed = input("Proceed?: ")
    if proceed.lower == "yes":
        ans = input("What is " + str(random1) + " * " + str(random2) + "?: ")
        if int(ans) == correct:
            print("Correct!")
            list_of_correct.append(str(random1 + ":" + str(random2) + ":" + str(ans)))
            print(ex())
        else:
            print("Incorrect!")
            list_of_wrong.append(str(random1) + ":" + str(random2) + ":" + str(ans))
    elif proceed.lower == "mfm":
        print(list_of_correct)
        print(list_of_wrong)


print(ex())

您将一个函数
与一个字符串
“yes”
进行比较。它们从来都不相同

您需要调用以下函数:

if proceed.lower() == "yes":
将输入转换为小写以便比较

print("".lower) # <built-in method lower of str object at 0x7fc134277508>
打印(“.lower”)#

比较一个函数
继续。将
与字符串
'yes'
放低-它们从来都不一样

您需要调用以下函数:

if proceed.lower() == "yes":
将输入转换为小写以便比较

print("".lower) # <built-in method lower of str object at 0x7fc134277508>
打印(“.lower”)#
固定代码

import random

random1 = random.randint(1, 11)
random2 = random.randint(1, 11)
correct = random1 * random2
list_of_correct = []
list_of_wrong = []


def ex():
    proceed = input("Proceed?: ")
    if proceed.lower() == "yes":
        ans = input("What is " + str(random1) + " * " + str(random2) + "?: ")
        if int(ans) == correct:
            print("Correct!")
            list_of_correct.append(str(random1 + ":" + str(random2) + ":" + str(ans)))
            print(ex())
        else:
            print("Incorrect!")
            list_of_wrong.append(str(random1) + ":" + str(random2) + ":" + str(ans))
    elif proceed.lower() == "mfm":
        print(list_of_correct)
        print(list_of_wrong)


print(ex())
固定代码

import random

random1 = random.randint(1, 11)
random2 = random.randint(1, 11)
correct = random1 * random2
list_of_correct = []
list_of_wrong = []


def ex():
    proceed = input("Proceed?: ")
    if proceed.lower() == "yes":
        ans = input("What is " + str(random1) + " * " + str(random2) + "?: ")
        if int(ans) == correct:
            print("Correct!")
            list_of_correct.append(str(random1 + ":" + str(random2) + ":" + str(ans)))
            print(ex())
        else:
            print("Incorrect!")
            list_of_wrong.append(str(random1) + ":" + str(random2) + ":" + str(ans))
    elif proceed.lower() == "mfm":
        print(list_of_correct)
        print(list_of_wrong)


print(ex())

更新了一个打字错误@wjandrea.更新了一个打字错误@wjandrea。