简体   繁体   English

在 Python 上没有询问任何问题时,如何阻止我的 Magic 8 Ball 程序生成随机数?

[英]How do I stop my Magic 8 Ball Program from generating a random number when no question is asked on Python?

I have modified my 8-Ball program on Python to perform different operations based on whether a name is entered or not, if a question is not asked or not, etc.我在 Python 上修改了我的 8-Ball 程序,以根据是否输入名称、是否未询问问题等执行不同的操作。

Everything works perfectly except that I can't figure how to turn off the random number generator which keeps spitting out a new correlating answer even when no question has been asked.一切都很完美,只是我不知道如何关闭随机数生成器,即使没有提出任何问题,它也会不断吐出新的相关答案。

Here is my code entered with no question, if you run the program you will see both the message that the user did not enter in their question in addition to the randomly generated answer.这是我毫无疑问地输入的代码,如果您运行该程序,除了随机生成的答案之外,您还会看到用户没有在他们的问题中输入的消息。 Can you help?你能帮我吗?

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'

You simply need to indent the answer block so it's inside of the else :您只需要缩进答案块,使其位于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:
        . . .

With how you have your code setup, the else block will only run when they enter a question, so that's where you should put code that you want to only run when they've entered a question.根据您的代码设置方式, else块只会在他们输入问题时运行,因此您应该在此处放置仅在他们输入问题时才运行的代码。

i think you just need to insert the answers to the correct if in this case it is the 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'

You can do this with a simple if statement.你可以用一个简单的 if 语句来做到这一点。 Just do all these stuff when:只需在以下情况下执行所有这些操作:

if question != '':
  #Code Here#
else:
  print('Sorry you did not asked any question!!! Try Next time')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM