1
votes

I'm attempting to program a Magic 8-Ball program and the program works, but I am trying to get it to recognize when the user hasn't input the name AND the question.

import random

name = input('What is your name?: ')
question = input('Which yes or no question do you wish to ask?: ')
answer = ''
random_number = random.randint(1, 9)

**if name == '':
    print('Question: ' + question)
elif name, question == '':
    print('You need to ask a question')
else:
    print(name + ' asks: ' + question)**

if random_number == 1:
    answer = 'Yes - Definetly'
elif random_number == 2:
    answer = 'It is decidedly so.'
elif random_number == 3:
    answer = 'Without a doubt.'
elif random_number == 4:
    answer = 'Reply hazy, try again.'
elif random_number == 5:
    answer = 'Ask again later.'
elif random_number == 6:
    answer = 'Better not tell you now.'
elif random_number == 7:
    answer = 'My sources say no.'
elif random_number == 8:
    answer = 'Outlook not so good.'
elif random_number == 9:
    answer = 'Very doubtful.'
else:
    answer = '404 Error! Magic 8-Ball Not Working, Try Again Later.'

print("Magic 8-Ball's Answer: " + answer)

The code in bold is what I am having issues with. If the user doesn't input a name or a question then I want to print 'Ask a question'

1
name == '' or question == '' or just drop the name check, you know it is not '' already because of first if. - matszwecja
There is no code in bold, just invalid syntax. - mkrieger1

1 Answers

0
votes

You can do individual checks for question and combine them with an and statement to do both checks on the same line.

name = input('What is your name?: ')
question = input('Which yes or no question do you wish to ask?: ')

if name == '':
    print('Question: ' + question)
elif name == '' and question == '':
    print('You need to ask a question')
else:
    print(name + ' asks: ' + question)

This will print "You need to ask a question" if both name and question are empty - but keep in mind that you are checking if name is = '' twice and it's best to keep code concise.

Another option you can try is

if not name and not question:
    print("you need to ask a question")

because an empty string automatically evaluates to false so we can negate the false to true using not. This will give you the same result.