有没有办法让这个 Python 函数 运行 更多的时间间隔?
Is there any way to make this Python function run for more intervals?
我是 python 的新手,刚刚编写了这段非常简单的代码。最后,这是我做的东西,只是为了弄湿我的脚,让我的头沉浸在编码中。 Python 是我的第一语言,几天前我开始学习它。
是的,我知道这是一个非常迂回的编码方法选择,这也是我自己制作的第一个超过几行的东西。所以,最后,有没有什么办法可以让函数 Question 运行 更多次,而不会 运行ning 出现返回变量的问题?我不确定这是否真的会成为一个问题,或者我现在太累了,看不出原因。我知道我需要为结果部分创建更多 if 语句。那是显而易见的。
name = raw_input("Please enter you preferred name: ")
print "Welcome, %r, to the general stupidity quiz." % name
raw_input("\n\tPlease press any button to continue")
question1 = "\n\nWhat is the equivilent of 2pi in mathmatics? "
answer1 = "tao"
answer1_5 = "Tao"
question2 = "\nWhat is 2 + 2? "
answer2 = "4"
w = 0
def Question(question, answerq, answere, inputs):
user_answer = raw_input(question)
if user_answer in [str(answerq), str(answere)]:
print "Correct!"
x = inputs + 1
return x
else:
print "False!"
x = inputs
return x
x = Question(question1, answer1, answer1_5, w)
x = Question(question2, answer2, answer2, x)
print "\nYou got " + str(x) + " questions correct..."
if x == 2:
print "You're not stupid!"
elif x == 1:
print "You're not smart!"
else:
print "I hate to tell you this...but..."
raw_input()
我在末尾添加了一个 raw_input() 所以我的 cmd window 不会关闭。我知道我可以使用 ubuntu(最近卸载了它),我也可以只 运行 使用 cmd window 的代码,但这只是我标记到最后的东西。
欢迎编程!很难说出你到底在做什么,但在我看来,你希望能够提出一堆不同的问题,而不必每次都编写更多代码。
您可以做的一件事是将 questions/answers 列表放入 list
:
quiz = [("what is one + one? ", "2"), ("what is 2 + 2? ", "4")]
这里我们只允许一个正确答案,所以我们需要更改 Question()
函数(我们仍然可以通过调用 answer.lower()
来接受 "Tao" 和 "tao" 以删除大小写问题):
def Question(question, answer):
user_answer = raw_input(question)
if user_answer.lower() == answer:
print "Correct!"
x = inputs + 1
return x
else:
print "False!"
x = inputs
return x
现在我们要做的就是为 quiz
列表中的每个问题调用问题函数:
for q in quiz:
Question(q[0], q[1])
您的目标应该是让您的功能独立并只做一件事。您的函数检查用户输入问题的正确性并递增计数器。这显然是两件事。我将把计数器的递增移出函数:
def question(question, answerq, answere):
user_answer = raw_input(question)
if user_answer in [str(answerq), str(answere)]:
print "Correct!"
return True
else:
print "False!"
return False
x = 0
if question(question1, answer1, answer1_5):
x += 1
if question(question2, answer2, answer2):
x += 1
现在我将通过更改一些更有意义的名称来稍微清理一下它,而不是假设每个问题总是恰好有两个答案:
def question(question, correct_answers):
user_answer = raw_input(question)
if user_answer in correct_answers:
print "Correct!"
return True
else:
print "False!"
return False
correct_count = 0
if question(question1, [answer1, answer1_5]):
correct_count += 1
if question(question2, [answer2, answer2]):
correct_count += 1
要了解更多信息,请查看编码最佳实践和编码风格。我建议阅读 PEP8 for Python.
我是 python 的新手,刚刚编写了这段非常简单的代码。最后,这是我做的东西,只是为了弄湿我的脚,让我的头沉浸在编码中。 Python 是我的第一语言,几天前我开始学习它。
是的,我知道这是一个非常迂回的编码方法选择,这也是我自己制作的第一个超过几行的东西。所以,最后,有没有什么办法可以让函数 Question 运行 更多次,而不会 运行ning 出现返回变量的问题?我不确定这是否真的会成为一个问题,或者我现在太累了,看不出原因。我知道我需要为结果部分创建更多 if 语句。那是显而易见的。
name = raw_input("Please enter you preferred name: ")
print "Welcome, %r, to the general stupidity quiz." % name
raw_input("\n\tPlease press any button to continue")
question1 = "\n\nWhat is the equivilent of 2pi in mathmatics? "
answer1 = "tao"
answer1_5 = "Tao"
question2 = "\nWhat is 2 + 2? "
answer2 = "4"
w = 0
def Question(question, answerq, answere, inputs):
user_answer = raw_input(question)
if user_answer in [str(answerq), str(answere)]:
print "Correct!"
x = inputs + 1
return x
else:
print "False!"
x = inputs
return x
x = Question(question1, answer1, answer1_5, w)
x = Question(question2, answer2, answer2, x)
print "\nYou got " + str(x) + " questions correct..."
if x == 2:
print "You're not stupid!"
elif x == 1:
print "You're not smart!"
else:
print "I hate to tell you this...but..."
raw_input()
我在末尾添加了一个 raw_input() 所以我的 cmd window 不会关闭。我知道我可以使用 ubuntu(最近卸载了它),我也可以只 运行 使用 cmd window 的代码,但这只是我标记到最后的东西。
欢迎编程!很难说出你到底在做什么,但在我看来,你希望能够提出一堆不同的问题,而不必每次都编写更多代码。
您可以做的一件事是将 questions/answers 列表放入 list
:
quiz = [("what is one + one? ", "2"), ("what is 2 + 2? ", "4")]
这里我们只允许一个正确答案,所以我们需要更改 Question()
函数(我们仍然可以通过调用 answer.lower()
来接受 "Tao" 和 "tao" 以删除大小写问题):
def Question(question, answer):
user_answer = raw_input(question)
if user_answer.lower() == answer:
print "Correct!"
x = inputs + 1
return x
else:
print "False!"
x = inputs
return x
现在我们要做的就是为 quiz
列表中的每个问题调用问题函数:
for q in quiz:
Question(q[0], q[1])
您的目标应该是让您的功能独立并只做一件事。您的函数检查用户输入问题的正确性并递增计数器。这显然是两件事。我将把计数器的递增移出函数:
def question(question, answerq, answere):
user_answer = raw_input(question)
if user_answer in [str(answerq), str(answere)]:
print "Correct!"
return True
else:
print "False!"
return False
x = 0
if question(question1, answer1, answer1_5):
x += 1
if question(question2, answer2, answer2):
x += 1
现在我将通过更改一些更有意义的名称来稍微清理一下它,而不是假设每个问题总是恰好有两个答案:
def question(question, correct_answers):
user_answer = raw_input(question)
if user_answer in correct_answers:
print "Correct!"
return True
else:
print "False!"
return False
correct_count = 0
if question(question1, [answer1, answer1_5]):
correct_count += 1
if question(question2, [answer2, answer2]):
correct_count += 1
要了解更多信息,请查看编码最佳实践和编码风格。我建议阅读 PEP8 for Python.