Python if语句中的输入未按预期工作
if语句中的输入无效。我使用的是Python3,问题在于定义函数中的第一个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.
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。