What's wrong with my code? It prints the same string twice

Hello! I’m taking this quiz and I got stuck at the very end. In fact, I get the same string printed twice as you can see:

I’d really appreciate if you could check out my code and let me know what is wrong. Thank you!

import random

name = "Giulia"
question = "Will it be sunny tomorrow?"
answer = ""
random_number = random.randint(1, 9)

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."
else:
  answer = "Error"

if question == "":
  print("ask something")
else:
  print(name + " asks: " + question)

if name == "":
  print("Question: " + question)
else:
  print(name + " asks: " + question)
  print("Magic 8-Ball's answer: " + answer)


Double check the two else statements.
Ex:

2 Likes

I would review your conditionals.

If you ever want to check how something is evaluating, you can print it’s result

#for example
print(question == "")

this can be useful in debugging conditionals.

2 Likes

Also, try commenting out the code logic if no name is provided.
What happens?

1 Like

I’m sorry guys, I feel really stupid at the moment. I did all the exercise again and I get the same (wrong) result. I’m afraid I couldn’t make the most of your helpful suggestions because English isn’t my mother tongue and I’m at the very beginning with coding, so I probably don’t know how some English terms are used in this field. Eg. “check how something is evaluating” @toastedpitabread is blurry to me, but also the idea of “commenting out” @lisalisaj the code logic isn’t clear.

My apologies for being unclear.
It prints out twice because the conditions are met with both of the else statements that I pasted from your code above.

By commenting out code I mean hiding the code (it won’t run) by using a # or, like so by using 3 apostrophes at the beginning & end of the code block that you don’t want to run:

#logic if no name provided
'''if name == "":
  print("Question: " + question)
else:
  print(name + " asks: " + question)
  print("Magic 8-Ball's answer: " + answer) '''

See here for more about comments in code.

1 Like

“check how something is evaluating”

if question == "": # the compiler evaluates this to True or False

however, sometimes we don’t know how the compiler “feels” about a certain statement so we can say

print(question == "") # prints true or false

Why is this useful?
Let’s say we have a value and we want to check it against a condition:

if condition == 4:
    print("this is the condition I want")
else:
    print("this is not the condition I want")

Let’s say we know we have a 4 for our condition but it’s still not printing… we can confirm the evaluation with

print(condition == 4) # this is giving us false
if condition == 4:
    print("this is the condition I want")
else:
    print("this is not the condition I want")

so then we might check the type (i.e.: string, integer, float):

print(type(condition)) # if condition == '4', this will return str
if condition == 4:
    print("this is the condition I want")
else:
    print("this is not the condition I want")

So one potential problem here is that we correctly got our code to have condition to be some 4 but it was unfortunately of type str ("4" ) instead of integer (4).
The point is to have more tools to set up points in your code where you can check the logic of what is happening.

3 Likes

I use this link for situations like this: visualize python
It allows you to step through the code execution, it’s a great tool for debugging.

4 Likes

THANK YOU VEEEERY MUCH! I appreciate the time you dedicated to my question. This is a great community :slight_smile:

2 Likes