https://www.codecademy.com/courses/learn-python-3/projects/python-magic-8-ball
I was solving the Magic 8 Ball question 14. I did it a bit differently than the answer given in the hint. Can anyone just have a look on my code and give me feedback.
Thank you in advance.
Please post your formatted code.
https://discuss.codecademy.com/t/how-to-ask-good-questions-and-get-good-answers/77980
I apologise for the inconvenience caused. Below is the code posted as formatted text.
Here is the link to the question -
https://www.codecademy.com/courses/learn-python-3/projects/python-magic-8-ball
import random
name = ""
question = ""
answer = ""
random_number = random.randint(1, 15)
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 += "You may rely on it."
elif random_number == 11:
answer += "As I see it, yes."
elif random_number == 12:
answer += "Cannot predict now."
elif random_number == 13:
answer += "Don't count on it."
elif random_number == 14:
answer += "My reply is no."
elif random_number == 15:
answer += "Concentrate and ask again."
else:
answer += "Error"
if name == "" and question == "":
print("You have not asked a question.")
elif name == "":
print("Question: ", question)
print("Magic 8-Ball's answer: ", answer)
else:
print(name, "asks: ", question)
print("Magic 8-Ball's answer: ", answer)
#if question == "":
#print("You have not asked a question.")
#else:
#print("Magic 8-Ball's answer: ", answer)
Hi 16bme1040,
Your code looks great! If you want to take it a step further, you should consider assigning name
and question
as an input()
functions like so:
name = input("What is your name: ")
question = input("What is your question: ")
Also, on line 39, technically you only need to check if question
is empty, and not both name
and question
like so:
if question == "":
print("You have not asked a question.")
Again, great job!
Thank you again very much