创建一个程序,用于计算用户正确键入字母所需的时间。它不会停止计时器,直到他们正确键入它,如果他们犯了错误,它将显示一条消息,他们将不得不尝试再次键入它。一旦他们成功地设法输入正确的字母,它将告诉他们花了多长时间,然后问他们是否想再次尝试,看看他们是否可以打败他们以前的时间。
我成功完成了第一部分:
import time
starttime = time.time()
def ask_question1():
answer1= input("Type in the alphabet as fast as you can then press enter: ")
return answer1
if __name__=="__main__":
answer1=ask_question1()
while answer1 != "abcdefghijklmnopqrstuvwxyz":
print("You made a mistake. ")
answer1=input("Try again: ")
endtime=time.time()
print("It took you ",round(endtime-starttime, 2), " seconds")
但是我无法再问这个问题并重复这个过程。
答案 0 :(得分:0)
您似乎需要拨打ask_question1
而不是answer1=input("Try again: ")
答案 1 :(得分:0)
使用raw_input()而不是input(),这会将其作为字符串返回。
import time
starttime = time.time()
def ask_question1():
return raw_input("Type in the alphabet as fast as you can then press enter: ")
if __name__=="__main__":
while ask_question1() != "abcdefghijklmnopqrstuvwxyz":
print("You made a mistake. ")
endtime=time.time()
print("It took you ",round(endtime-starttime, 2), " seconds")
答案 2 :(得分:0)
这应该有用;使用raw_input
并递归调用函数。
import time
starttime = time.time()
def ask_question1():
answer1= raw_input("Type in the alphabet as fast as you can then press enter: ")
return answer1
if __name__=="__main__":
answer1=ask_question1()
while answer1 != "abcdefghijklmnopqrstuvwxyz":
print("You made a mistake. ")
answer1=ask_question1()
endtime=time.time()
print("It took you ",round(endtime-starttime, 2), " seconds")
答案 3 :(得分:0)
import time
starttime = time.time()
def ask_question1():
return raw_input("Type in the alphabet as fast as you can then press enter: ")
if __name__=="__main__":
while ask_question1() != "abcdefghijklmnopqrstuvwxyz":
print("You made a mistake.Try again: ")
endtime=time.time()
print("It took you ",round(endtime-starttime, 2), " seconds")
答案 4 :(得分:0)
{{1}}
你可以尝试这样的事情。
答案 5 :(得分:0)
import time
starttime = time.time()
def ask_question1():
answer1= raw_input("Type in the alphabet as fast as you can then
press enter: ")
return replying_question1(answer1)
def replying_question1(answer):
while answer in "abcdefghijklmnopqrstuvwxyz":
endtime=time.time()
print("It took you ",round(endtime-starttime, 2), " seconds")
return round(endtime-starttime, 2)
print("You made a mistake. ")
ask_question1()
if __name__=="__main__":
ask_question1()
输出将是: 失败:
Type in the alphabet as fast as you can then press enter: 3
You made a mistake.
正确:
Type in the alphabet as fast as you can then press enter: e
('It took you ', 3.32, ' seconds')
而不是raw_input只有在使用python 3时才能使用input()。
答案 6 :(得分:0)
不需要ask_question1
功能,您可以使用input
在Python 3中或在Python 2中raw_input
。
您无需编写"abcdefghijklmnopqrstuvwxyz"
,您可以使用
Python 2中的string.lowercase
或Python中的string.ascii_lowercase
你可以永远做一段时间;)发牢骚while True
并使用brake
退出循环。
Python 2:
import time
import string
if __name__=="__main__":
starttime = time.time()
answer1 = raw_input("Type in the alphabet as fast as you can then press enter: ")
while True:
if answer1 != string.lowercase:
print("You made a mistake. ")
answer1 = raw_input("Try again: ")
else:
endtime = time.time()
print("It took you " + str(round(endtime-starttime, 2)) + " seconds")
break
Python 3:
import time
import string
if __name__=="__main__":
starttime = time.time()
answer1 = input("Type in the alphabet as fast as you can then press enter: ")
while True:
if answer1 != string.ascii_lowercase:
print("You made a mistake. ")
answer1=input("Try again: ")
else:
endtime=time.time()
print("It took you ",round(endtime-starttime, 2), " seconds")
break