![](/img/trans.png)
[英]Question about the magic 8 ball program with a list from Automate the Boring Stuff with Python
[英]How do I stop my Magic 8 Ball Program from generating a random number when no question is asked on Python?
我在 Python 上修改了我的 8-Ball 程序,以根據是否輸入名稱、是否未詢問問題等執行不同的操作。
一切都很完美,只是我不知道如何關閉隨機數生成器,即使沒有提出任何問題,它也會不斷吐出新的相關答案。
這是我毫無疑問地輸入的代碼,如果您運行該程序,除了隨機生成的答案之外,您還會看到用戶沒有在他們的問題中輸入的消息。 你能幫我嗎?
import random
# who is playing the game
name = 'Chantel'
# ask a yes or no question
question = ''
# answer to question
answer = ''
# random answer generator
random_number = random.randint(1, 12)
# print(random_number) tester
# run a test of the program
# adjusts answer if there is no name entered or prints out an alternate message if no question is
asked
if name == '':
print('Question: ' + question)
if question == '':
print('You forgot to enter in your question for the Magic 8-Ball!')
else:
print(name + " asks: " + question)
print('Magic 8-Ball Answer:')
# random answers generated
if random_number == 1:
print('Yes, definitely')
elif random_number == 2:
print('It is decidedly so.')
elif random_number == 3:
print('Without a doubt.')
elif random_number == 4:
print('Reply hazy, try again.')
elif random_number == 5:
print('Ask again later.')
elif random_number == 6:
print('Better not tell you now.')
elif random_number == 7:
print('My sources say no.')
elif random_number == 8:
print('Outlook not so good.')
elif random_number == 9:
print('Very doubtful.')
elif random_number == 10:
print('I would keep trying.')
elif random_number == 11:
print('I would not doubt it.')
elif random_number == 12:
print('100%')
else:
answer = 'Error'
您只需要縮進答案塊,使其位於else
內部:
else:
print(name + " asks: " + question)
print('Magic 8-Ball Answer:')
# ^ Aligned with the above lines now ^
if random_number == 1:
print('Yes, definitely')
elif random_number == 2:
print('It is decidedly so.')
elif random_number == 3:
. . .
根據您的代碼設置方式, else
塊只會在他們輸入問題時運行,因此您應該在此處放置僅在他們輸入問題時才運行的代碼。
我認為你只需要插入正確的答案,如果在這種情況下它是其他的:
import random
# who is playing the game
name = 'Chantel'
# ask a yes or no question
question = ''
# answer to question
answer = ''
# random answer generator
random_number = random.randint(1, 12)
# print(random_number) tester
# run a test of the program
# adjusts answer if there is no name entered or prints out an alternate message if no question is
asked
if name == '':
print('Question: ' + question)
if question == '':
print('You forgot to enter in your question for the Magic 8-Ball!')
else:
print(name + " asks: " + question)
print('Magic 8-Ball Answer:')
# random answers generated
if random_number == 1:
print('Yes, definitely')
elif random_number == 2:
print('It is decidedly so.')
elif random_number == 3:
print('Without a doubt.')
elif random_number == 4:
print('Reply hazy, try again.')
elif random_number == 5:
print('Ask again later.')
elif random_number == 6:
print('Better not tell you now.')
elif random_number == 7:
print('My sources say no.')
elif random_number == 8:
print('Outlook not so good.')
elif random_number == 9:
print('Very doubtful.')
elif random_number == 10:
print('I would keep trying.')
elif random_number == 11:
print('I would not doubt it.')
elif random_number == 12:
print('100%')
else:
answer = 'Error'
你可以用一個簡單的 if 語句來做到這一點。 只需在以下情況下執行所有這些操作:
if question != '':
#Code Here#
else:
print('Sorry you did not asked any question!!! Try Next time')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.