Python 等待错误
我试着找一个好的代码行,让Python在执行下一个代码块之前等待,但我找到的都不管用。下面是一个代码示例:
#This is the eighth Question.
print "Q8. What is one of the main bosses in 'Super Smash Brothers Brawl' that is also a boss in 'Kirby the Amazing Mirror?'"
print "1) Krazy hand."
print "2) Mister hand."
print "3) Ganondorf."
print "4) Yoshi."
choice = input("Answer=")
if choice ==1:
print "THAT IS INCORRECT! 'Krazy hand' is SUPPOSED to be spelled 'Crazy hand'!"
print ""
elif choice ==2:
print "INCORRECT! 'Mister hand' is supposed to be spelled 'Master hand'!"
print ""
elif choice ==3:
print "Ganondorf is in the LEGEND OF ZELDA!"
print ""
elif choice ==4:
print "Yoshi isn't in Kirby!!!"
print ""
elif choice> 4 or choice < 1:
print "Go away if you want to fail."
print ""
我希望这个“等待”的代码能在执行下面的代码之前等10秒。
print ""
other = (raw_input("Would you like to continue without choosing a answer?:"))
if other == "yes":
print "Haha! Nicely done! None of those answers were correct! This was a ULTRA QUESTION in disguise!"
print ""
score = score+3
if other == "no":
print "Well, we have to move on anyway. Too bad you didn't say yes......."
print ""
谢谢!任何帮助都非常感谢!
更新1:
这是所有的代码:
#This is the script for TUKT. DO NOT DELETE
score = 0
print ""
print "Welcome to the Ultimate Kirby Test."
print ""
print "Type 'yes' to begin. Type 'no' to quit. Type 'info' for how to play."
print ""
begin = (raw_input("Would you like to begin?:"))
if begin == "yes":
print ""
print "Alright then! Let's start, shall we?"
if begin == "no":
quit()
if begin == "info":
print ""
print "Type the number of the answer that you think is correct after 'Answer='. Sometimes, ULTRA QUESTIONs will pop up. These are worth 3 points!! At the end of the test, I will show you your score. You must get over 7 right to pass. Good luck!"
#This is the first Question.
print "Q1. What color is Kirby?"
print "1) Black."
print "2) Blue."
print "3) Pink."
print "4) Technically, his color changes based on the opponent he swallows."
choice = input("Answer=")
if choice ==1:
print ""
print "Incorrect."
print ""
elif choice ==2:
print ""
print "Incorrect."
print ""
elif choice ==3:
print ""
print "Tee hee.... I fooled you!"
print ""
elif choice ==4:
print ""
print "Nice job! You saw through my trick!"
print ""
score = score+1
elif choice > 4 or choice < 1:
print ""
print "That is not a valid answer."
print ""
#This is the second Question.
print "Q2. Who turns Kirby into yarn? (Kirby's Epic Yarn)"
print "1) Elmo."
print "2) The terrible cotton wizard named the yarn lord."
print "3) A magician named Yin-Yarn."
print "4) King Dedede."
choice = input("Answer=")
if choice ==1:
print ""
print "Don't be silly. Elmo is copyrighted!"
print ""
elif choice ==2:
print ""
print "I can't believe you fell for that!"
print ""
elif choice ==3:
print ""
print "Nicely done!"
print ""
score = score+1
elif choice ==4:
print ""
print "Ha! Nice try."
print ""
elif choice > 4 or choice < 1:
print ""
print "You have got to be joking."
print ""
#This is the third Question.
print "Q3. Who is the villan in Kirby's Adventure?"
print "1) King Dedede."
print "2) The Nightmare."
print "3) Meta Knight."
print "4) All of the above."
choice = input("Answer=")
if choice ==1:
print ""
print "Too bad!"
print ""
elif choice ==2:
print ""
print "I tricked you! Muahahaha!"
print ""
elif choice ==3:
print ""
print "FALSE!!"
print ""
elif choice ==4:
print ""
print "Let me think...................YES!"
print ""
score = score+1
elif choice > 4 or choice < 1:
print ""
print "You despise me."
print ""
#This is the fourth Question.
print "Q4. In which game can Kirby combine the powers of his enemies?"
print "1) Kirby's Dream Land 2."
print "2) Kirby 64: The Crystal Shards."
print "3) Kirby and the seven stones."
print "4) Kirby Nightmare in Dreamland."
choice = input("Answer=")
if choice ==1:
print ""
print "Nope. Sorry!"
print ""
elif choice ==2:
print ""
print "Yes! That is correct! Nicely done!"
print ""
score = score+1
elif choice ==3:
print ""
print "That isn't even a real game! (hence not capitalizing it.)"
print ""
elif choice ==4:
print ""
print "No. You should be more knowledgable about Kirby games!"
print ""
elif choice> 4 or choice < 1:
print ""
print "Hmph! You should actually 'try' next time!"
print ""
#This is the fifth Question.
print "Q5. ULTRA QUESTION! There was a voting poll to see the top ten Kirby games! Which one do you think came up on top? The choices are: Kirby 64 The Crystal Shards, Kirby's Epic Yarn, Kirby Super Star Ultra, Kirby's Return to Dream Land, Kirby and The Amazing Mirror, Kirby Squeak Squad, Kirby Air Ride, Kirby's Dream Land 2, Kirby's Dream Land 3. Please type your guess in all LOWERCASE LETTERS and NO SPACES and NO PUNCTUATION! DO NOT SPELL IT WRONG. IF YOU DO, THE QUESTIONS WILL GO ON. Start!"
print ""
begin = (raw_input("What is your guess?:"))
if begin == "kirby64thecrystalshards":
print ""
print "Sorry that is incorrect."
print ""
if begin == "kirbysepicyarn":
print ""
print "Sorry that is incorrect."
print ""
if begin == "kirbysuperstarultra":
print ""
print "Sorry that is incorrect."
print ""
if begin == "kirbysreturntodreamland":
print ""
print "Hurrah! You got it correct!"
print ""
score = score+3
if begin == "kirbyandtheamazingmirror":
print ""
print "Sorry that is incorrect."
print ""
if begin == "kirbysqueaksquad":
print ""
print "Sorry that is incorrect."
print ""
if begin == "kirbyairride":
print ""
print "Sorry that is incorrect."
print ""
if begin == "kirbysdreamland2":
print ""
print "Sorry that is incorrect."
print ""
if begin == "kirbysdreamland3":
print ""
print "Sorry that is incorrect."
print ""
#This is the sixth Question.
print "Q6. Identify the picture!"
print ""
print ""
print ""
print " ------------------ "
print " / \ "
print " | | "
print " | | | |"
print " | |"
print " \ / "
print " /------------------\ "
print " |_______/ \________| "
print ""
print "1) A Kirby."
print "2) A walking box."
print "3) ?"
print "4) My mom."
choice = input("Answer=")
if choice ==1:
print "FAIL!!! A Kirby has arms!"
print ""
elif choice ==2:
print "That is correct! It's not a Kirby because it doesn't have arms!"
print ""
score = score+1
elif choice ==3:
print "No. That is incorrect. No points for you!"
print ""
elif choice ==4:
print "By the way, THAT IS NOT YOUR MOM!"
print ""
elif choice> 4 or choice < 1:
print "Wow. Really?"
print ""
#This is the seventh Question.
print "Q7. Meta Knight really is:"
print "1) A Kirby."
print "2) A robot."
print "3) A Waddle Dee."
print "4) None of the above."
choice = input("Answer=")
if choice ==1:
print "Nice try. YOU ARE CORRECT!"
print ""
score = score+1
elif choice ==2:
print "INCORRECT! Nice try."
print ""
elif choice ==3:
print "HA! No way."
print ""
elif choice ==4:
print "Heh! It seems as if I have fooled you!"
print ""
elif choice> 4 or choice < 1:
print "Why don't you read the instructions next time?"
print ""
#This is the eighth Question.
print "Q8. What is one of the main bosses in 'Super Smash Brothers Brawl' that is also a boss in 'Kirby the Amazing Mirror?'"
print "1) Krazy hand."
print "2) Mister hand."
print "3) Ganondorf."
print "4) Yoshi."
choice = input("Answer=")
if choice ==1:
print "THAT IS INCORRECT! 'Krazy hand' is SUPPOSED to be spelled 'Crazy hand'!"
print ""
elif choice ==2:
print "INCORRECT! 'Mister hand' is supposed to be spelled 'Master hand'!"
print ""
elif choice ==3:
print "Ganondorf is in the LEGEND OF ZELDA!"
print ""
elif choice ==4:
print "Yoshi isn't in Kirby!!!"
print ""
elif choice> 4 or choice < 1:
print "Go away if you want to fail."
print ""
time.sleep(10)
print ""
other = (raw_input("Would you like to continue without choosing a answer?:"))
if other == "yes":
print "Haha! Nicely done! None of those answers were correct! This was a ULTRA QUESTION in disguise!"
print ""
score = score+3
if other == "no":
print "Well, we have to move on anyway. Too bad you didn't say yes......."
print ""
#This is the ninth question.
print "THIS IS A SAMPLE QUESTION."
print ""
quit()
#DELETE THE UPPER 3 LINES OF CODE.
2 个回答
重要声明:
这不是你在这个问题中问的答案,所以请你还是参考Remolten的回复,因为那才是你问的问题的正确答案。
如果你不遵守上面的声明,我会删除我的回答。你被警告了:-D
说完声明后,我查了一下你关于这个游戏的其他两个问题(https://stackoverflow.com/questions/22889223/how-to-prompt-the-user-in-10-seconds-if-nothing-is-entered-in-python-on-mac 和 https://stackoverflow.com/questions/22889477/how-to-ask-a-person-for-input-but-running-a-whole-different-line-of-code-after-5,这两个问题已经被合理地屏蔽了),我想出了一个我确定能满足你需求的代码(我在使用Python 2.7和MacOs X 10.9.2的MacBook Air上测试过)。这段代码是基于这个回答,正如我在对你其他问题的评论中提到的那样,这段代码必须在Mac上工作(而且确实可以)。
我发这个的原因是因为你让我想起了我15年前写的第一个程序。那是一个选择游戏,和你想做的很相似(实际上,我当时也没有调用任何函数:-D),让我感到怀旧。你正在做的事情是个不错的练习,我不希望你因此而气馁。编程非常有趣,而Python是个很好的语言。
请试着理解我发的代码是怎么回事(先查查sys和select模块的文档)。这是提高自己的唯一方法。多上网查资料,阅读,阅读,再练习。
另外,一个很好的工具,可以让别人告诉你如何提高编程技能,就是把你的代码发到https://codereview.stackexchange.com/。在那里创建一个账户,问问如何改进你的代码。
最后一点:
在Stack Overflow(或其他stackexchange.com网站)发问题时,花点时间准确地解释你的问题。像是“不工作...坏了...坏掉了!!给我个解决办法!!”这样的说法对任何人都没有帮助(好吧,你并不是完全这样说的,但你得承认,关于Kirby游戏的第一个问题确实有点让人困惑)。
要记住,S.O.(StackOverflow)是一个非常知名的网站,它的答案很多时候会出现在谷歌的前几条结果中。如果你把问题表述得当,你会得到更好的答案,也能帮助将来遇到同样问题的人。
Stack Overflow上有很多天才(是的,不是我,但我知道有!!)在免费回答大家的问题(我说的是那些在维基百科上有文章的人,明白了吗?)。如果你好好利用,S.O.是个很棒的资源(可以看看这个,这个,并关注一下meta,了解StackOverflow的网络礼仪)。
好了,关于我这些老掉牙的故事就到此为止,下面是代码:
#!/usr/bin/env python
import sys, select
def timeout_input(message, timeout=10):
print "You have %s seconds to answer!" % timeout
if message:
sys.stdout.write(message)
sys.stdout.flush()
i, o, e = select.select( [sys.stdin], [], [], timeout )
if (i):
return sys.stdin.readline().strip()
return ""
# -----
score = 0
# @boatofturtles, put your other 7 questions
# --- HERE ---
# (I didn't want to copy all your code again)
#This is the eighth Question.
print "Q8. What is one of the main bosses in 'Super Smash Brothers Brawl' that is also a boss in 'Kirby the Amazing Mirror?'"
print "1) Krazy hand."
print "2) Mister hand."
print "3) Ganondorf."
print "4) Yoshi."
choice = timeout_input("Answer=", timeout=5)
if not(choice):
print ""
other = (raw_input("Would you like to continue without choosing a answer?:"))
if other == "yes":
print "Haha! Nicely done! None of those answers were correct! This was a ULTRA QUESTION in disguise!"
print ""
score = score+3
if other == "no":
print "Well, we have to move on anyway. Too bad you didn't say yes......."
print ""
elif int(choice) ==1:
print "THAT IS INCORRECT! 'Krazy hand' is SUPPOSED to be spelled 'Crazy hand'!"
print ""
elif int(choice) ==2:
print "INCORRECT! 'Mister hand' is supposed to be spelled 'Master hand'!"
print ""
elif int(choice) ==3:
print "Ganondorf is in the LEGEND OF ZELDA!"
print ""
elif int(choice) ==4:
print "Yoshi isn't in Kirby!!!"
print ""
elif int(choice)> 4 or int(choice) < 1:
print "Go away if you want to fail."
print ""
如果你一开始没有在5秒内提供答案,然后你对问题Would you like to continue without choosing a answer?
回答yes
,你将会看到以下输出:
Q8. What is one of the main bosses in 'Super Smash Brothers Brawl' that is also a boss in 'Kirby the Amazing Mirror?'
1) Krazy hand.
2) Mister hand.
3) Ganondorf.
4) Yoshi.
You have 5 seconds to answer!
Answer=
Would you like to continue without choosing a answer?:yes
Haha! Nicely done! None of those answers were correct! This was a ULTRA QUESTION in disguise!
如果你想要暂停一段时间,而不执行任何代码:
import time #Put this at the top of the script
time.sleep(10) #Put in the amount seconds