Conditional control flow statements, input

Hello friends, I am modifying the code in this ([https://www.codecademy.com/courses/learn-python-3/projects/python-magic-8-ball] exercise a bit.
I understood how conditional statements work, but I am trying to make this exercise such as, it should ask first to input ‘name’ and ‘question’ by user. And then move forward to random function and conditional statements. I tried using input() function in my code as well in first 2 lines. But it is not working as I want.

name = input()
question = input()
# name = 'Joe'
# question = 'Will India win this match?'
answer = ''
import random
random_number = random.randint(1,10)
print(random_number)

if random_number == 1:
  answer = 'Yes - definitely.'
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.'
elif random_number == 10:
  answer = 'Signs point to yes'
else:
  answer = 'Error'

if name == '':
  print('Question: ' + question)
else:
  print(name +' asks: '+ question)

if question == "":
  print("The Magic 8-Ball cannot provide a fortune unless you ask it something.")
else:
  print(name +' asks: '+ question)
  print("Magic 8-Ball's answer: "+ answer)

Please can you help me doing this?

The problem lies here:

You need to do something like this:

name = input("Pls enter your name:")
question = input('Will India win this match?")

The logic of input() is first to display whatever that’s put inside the bracket() to the screen, then captures whatever user input/type on keyboard ends by “enter” key.
The fact that you got nothing inside the input() function means nothing is displayed :rofl:. But, in fact it’s still waiting for user to input! If you type something following by “enter”, it should work and you need to perform twice since you have 2 input(). :grinning:

Subsequently, you can print out name & question to verify what you input:

print(name)
print(question)

Keep going! :+1:

1 Like

Thanks @ajax5260537031 for reply. I just want to try the scenario mentioned in last two if else statements in my code, if you see there is condition which states: if I don’t enter any name/question means empty string then perform operation inside the if statement. That means if I entered no name/question, it will show error message which I given ‘The Magic 8-Ball cannot provide a fortune unless you ask it something.’ That’s why I put empty function like input().
Also I am beginner in this, so if you know how to write code for the purpose I want, then please clarify how to do that?

hello darshanpawar63602471

Maybe you can specified what exactly is not working? As far I see, the code is working as expected. It will be good if you can give an example.

The only problem I see is when:
name is empty
question is not empty

if question == "":
  print("The Magic 8-Ball cannot provide a fortune unless you ask it something.")
else:
  print(name +' asks: '+ question)
  print("Magic 8-Ball's answer: "+ answer)

The outcome will look: (there’s a space before the asks)

 asks: win or not?
Magic 8-Ball's answer: <random number result>.
1 Like

@ajax5260537031 Finally after some digging, try and error, I got the code of my purpose.

name = input('enter your name: ')
question = input('what is your question: ')
# name = 'Joe'
# question = 'Will India win this match?'
answer = ''
import random
random_number = random.randint(1,10)
print(random_number)
if random_number == 1:
  answer = 'Yes - definitely.'
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.'
elif random_number == 10:
  answer = 'Signs point to yes'
else:
  answer = 'Error'

if name == '':
  print('Question: ' + question)
else:
  print(name +' asks: '+ question)

if question == "":
  print("The Magic 8-Ball cannot provide a fortune unless you ask it something.")
else:
  print("Magic 8-Ball's answer: "+ answer)

This was what I want my code to look like. Means I wanted to enter name/ question by input command, and also wanted to check that if I keep empty name/question then my if-else statements to work.
Thanks for support. :slightly_smiling_face: