制作一个while循环来请求重新启动我的脚本

时间:2017-03-14 10:58:23

标签: python-3.x

在我的代码中,我想询问我是否应该再次询问或最后关闭,我已经看到你可以使用while循环并要求继续或中断,但这些代码似乎都不适合我。我需要为学校项目添加这行代码。

import random
import time 


ans1="Without a doubt"
ans2="As I see it, yes"
ans3="Signs point to yes"
ans4="Better not tell you now"
ans5="Concentrate and ask again"
ans6="Don't count on it"
ans7="Very Doubtful"
ans8="My reply is no"
ans9="Reply hazy, try again"
ans10="Most Likely"
ans11="Very Doubtful"
ans12="Concentrate and ask again"




print("Welcome to my Magic 8 ball")
name=input("Please enter your name")
print ("Hi",name)

question = input("Hi, please ask me a question")
print("shaking...........")
time.sleep(1)
print("shaking...........")
time.sleep(1)
print("shaking...........")
time.sleep(1)
print("shaking...........")
time.sleep(1)
print ("My thought's lead too\n")

choice = random.randint(1,12)


if choice == 1:
    answer = ans1
elif choice == ans2:
    answer = ans2
elif choice == 3:
    answer = ans3
elif choice == 4:
    answer = ans4
elif choice == 5:
    answer = ans5
elif choice == 6:
    answer = ans6
elif choice == 7:
    answer = ans7
elif choice == 8:
    answer = ans8
elif choice == 9:
    answer = ans9
elif choice == 10:
    answer = 10
elif choice == 11:
    answer = 11
else:
    answer = ans12

print (answer)
input ("\n\nPress the ENTER key to finish")

2 个答案:

答案 0 :(得分:0)

您可以按如下方式添加while循环。这将询问用户是否想要提出另一个问题。如果未键入y,则while循环将停止:

import random
import time 

ans1 = "Without a doubt"
ans2 = "As I see it, yes"
ans3 = "Signs point to yes"
ans4 = "Better not tell you now"
ans5 = "Concentrate and ask again"
ans6 = "Don't count on it"
ans7 = "Very Doubtful"
ans8 = "My reply is no"
ans9 = "Reply hazy, try again"
ans10 = "Most Likely"
ans11 = "Very Doubtful"
ans12 = "Concentrate and ask again"

again = 'y'

while again == 'y':
    print("Welcome to my Magic 8 ball")
    name = input("Please enter your name: ")
    question = input("Hello {}, please ask me a question".format(name))

    for _ in range(4):
        print("shaking...........")
        time.sleep(1)

    print ("My thought's lead too\n")

    choice = random.randint(1, 12)

    if choice == 1:
        answer = ans1
    elif choice == ans2:
        answer = ans2
    elif choice == 3:
        answer = ans3
    elif choice == 4:
        answer = ans4
    elif choice == 5:
        answer = ans5
    elif choice == 6:
        answer = ans6
    elif choice == 7:
        answer = ans7
    elif choice == 8:
        answer = ans8
    elif choice == 9:
        answer = ans9
    elif choice == 10:
        answer = 10
    elif choice == 11:
        answer = 11
    else:
        answer = ans12

    print (answer)
    again = input("Would you like to ask another question? (Type y for yes) ")

您可以使用列表存储所有可能的答案,从而进一步改进代码。 random.choice()可用于从列表中选择随机条目:

import random
import time 

answers = [
    "Without a doubt",
    "As I see it, yes",
    "Signs point to yes",
    "Better not tell you now",
    "Concentrate and ask again",
    "Don't count on it",
    "Very Doubtful",
    "My reply is no",
    "Reply hazy, try again",
    "Most Likely",
    "Very Doubtful",
    "Concentrate and ask again"]

again = 'y'

while again == 'y':
    print("Welcome to my Magic 8 ball")
    name = input("Please enter your name: ")
    question = input("Hello {}, please ask me a question: ".format(name))

    for _ in range(4):
        print("shaking...........")
        time.sleep(1)

    print ("My thought's lead too\n")
    print (random.choice(answers))

    again = input("Would you like to ask another question? (Type y for yes) ")

答案 1 :(得分:0)

另外我想添加一段代码,所以如果我回复它就会说它很棒'如果他们回复Bad,那就说“哦不”'

import random
import time 

answers = [
    "Without a doubt",
    "As I see it, yes",
    "Signs point to yes",
    "Better not tell you now",
    "Concentrate and ask again",
    "Don't count on it",
    "Very Doubtful",
    "My reply is no",
    "Reply hazy, try again",
    "Most Likely",
    "Very Doubtful",
    "Concentrate and ask again"]

print("Welcome to my Magic 8 ball")
name = input("Please enter your name: ")
print ("{}, nice to meet you =p ".format(name))
age = input ("Please enter your age:")
print ("Really! You don't look that old xD")
Feeling = input ("How are you feeling today?")

again = 'y'

while again == 'y':
    question = input("{}, please ask me a question: ".format(name))

    print ("Shaking......\n") 
    time.sleep(1)
    print ("Shaking......\n")
    time.sleep(1)
    print ("Still with me?\n")
    time.sleep(1)
    print ("Think I have it\n")
    time.sleep(1)


    print ("My thought's lead too\n")
    print (random.choice(answers))

    again = input("Would you like to ask another question? (Type y for yes) ")