dimanche 16 mai 2021

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.

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'

'''




Aucun commentaire:

Enregistrer un commentaire