2017-03-14 57 views
-1

在我的代碼,我想它,問我是否應該在結束時再次或接近問,我已經看到了你可以使用while循環,並要求繼續進行或中斷,但這些代碼似乎都不適合我。我需要爲學校項目添加一行代碼。製作一個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") 
+0

顯示你有什麼用while循環嘗試。 – klutt

回答

0

您可以添加一個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) ") 

您還可以通過使用列表來存儲所有可能的答案改善你的代碼:如果他們不鍵入y while循環將停止。 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) ") 
0

另外我想補充的一段代碼,所以如果我他們回答好它說「偉大」,如果他們回答壞,它說「哦沒有」

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