Python waiting error

吃可爱长大的小学妹 提交于 2019-12-25 16:46:13

问题


I tried to find a good code line for making python wait before executing the next code block, but nothing I found worked. Here is a sample of the code below:

#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 ""

I want the 'wait' code to wait 10 seconds before executing the code below.

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 ""

Thanks! All help is much appreciated!

UPDATE 1: here is ALL of the code:

#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.

回答1:


If you want to wait without executing any code:

import time #Put this at the top of the script

time.sleep(10) #Put in the amount seconds



回答2:


@boatofturtles:

IMPORTANT DISCLAIMER:

This is not an answer to what you asked in this question, so please, keep your chosen answer to what Remolten replied, because that's the right answer to what you asked and the way you worded your question.

If you don't comply with the disclaimer above, I'll delete my answer. You've been warned :-D


Disclaimer said, after some digging and the reading of your other two questions related to this game you're doing (https://stackoverflow.com/questions/22889223/how-to-prompt-the-user-in-10-seconds-if-nothing-is-entered-in-python-on-mac and https://stackoverflow.com/questions/22889477/how-to-ask-a-person-for-input-but-running-a-whole-different-line-of-code-after-5 which have been righteously blocked) I have come up with some code that I know for sure does work the way you wanted (I'm on a MacBook Air with Python 2.7 and MacOs X 10.9.2) The code is based on this answer, which, as I already mentioned in a comment to one of your other questions, had to work in a Mac (and it does).

The reason I'm posting this (although it may be borderline wrong according to Stack Overflow's terms and intended usage) is because you reminded me of the first program I wrote... 15 years ago. It was a choices game pretty similar to what you're trying to do (as a matter of fact, I didn't even call a d*mn function either :-D ) and it made me feel nostalgic. What you're trying to do is a nice exercise and I wouldn't like for you to get discouraged. Programming is very fun and Python is a good language.

Please, try to understand what is going on in the code I posted (check the documentation of the sys and select modules, for starters). Is the only way to get better. Googling and reading, reading and practicing.

Also, a great tool so people can tell you how to improve your coding skills, is posting your code in https://codereview.stackexchange.com/ Create an account there and ask how you can improve your code.

Last thing:

When you post a question in Stack Overflow (or any other stackexchange.com site) take some time to explain your problem accurately. A doesn't work... it's broken... brokeeEEEeeen!! Show me a fix!! doesn't help anybody (ok, is not that you did exactly that, but you've got to recognize that this first question about the Kirby game was quite confusing).

Keep in mind that S.O.(StackOverflow) is a very well known site, whose answers appear among the first results in Google many, many times. If you formulate your question properly, you'll be getting better answers to your question and you will be helping other people that face your same issue in the future.

There's a lot of geniuses in Stack Overflow (yeah, not me, but I know there are!!) answering for free to everybody (I'm talking dudes with articles about them in the Wikipedia, aight?). S.O. is a great resource if you use it well (read this, this and keep an eye on the meta to learn about the netiquette in StackOverflow's net)

Ok, enough about my old timer's stories, here's the code:

#!/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 ""

If you initially don't provide an answer for 5 seconds, and then you reply yes to the question Would you like to continue without choosing a answer? you'll get the following output:

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!


来源:https://stackoverflow.com/questions/22887938/python-waiting-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!