Python Help

Hello there! I’m trying to expand on the python 3 8-ball project by adding a input() method to the name variable so that i can enter in any name. When I run the final project i get the name i wanted but my answer is printed right under it instead of in the “magic 8-ball’s answer:” spot. What part of my code should i take a look at?

Hi There,

In your code example, random_integer selected 7 and printed the output, “Yes…yes you are”, however, the variable answer was never assigned to anything expect an empty string. Therefore, you need to assign the variable answer to one of the output messages. For example:


import random

name = input("Enter your name: ")
answer = ''

random_integer = random.randint(1, 10)

if random_integer == 1:
    answer = 'Naaah'
elif random_integer == 2:
    answer == 'You eat a bowl of code'
# --- Continue the pattern:
else:
    answer = 'Error'

print(name, answer)

Now, when a random_integer is selected, that particular message will be assigned to the answer variable, and will display in the output. Let me know if this resolves your issue!

It’s giving me the answer before the name and then the answer afterwards. What im trying to do is enter in any name i want and then have it answer my question with the specified name.

I apologize, I mistyped something in my previous post and I cannot edit. The correct code should go something like this:


import random

name = input("Enter your name: ")
question = "Am I tough enough"?
answer = ''

random_integer = random.randint(1, 10)

if random_integer == 1:
    answer = 'Naaah'
elif random_integer == 2:  
    answer = 'You eat a bowl of code' 
# --- Continue the pattern:
else:
    answer = 'Error'

print(f"{name} ask {question}")
print(f"Magic 8-ball's answer: {answer}")

Again, I apologize for any confusion. Let me know how that works!

1 Like

That kind of worked for me! Im still having trouble figuring out how to get 3 and 4 to switch places though. I’ve tried rearranging the code but that doesn’t seem to work for me.

image

Hey There,

I’m glad you’re to hear this almost worked for you! If you feel comfortable, would you mind positing your code (not a screen shot)? Look forward t your response!

1 Like

import random

name = input(“Enter your name:”)

question = “Am I tough enough?”

random_integer = random.randint(1, 10)

answer = “”

if random_integer == 1:

print(“Naaaah”)

elif random_integer == 2:

print(“You eat a bowl of nails w/o any milk?”)

elif random_integer == 3:

print(“How many bones haven’t you broken?”)

elif random_integer == 4:

print(“Try again…”)

elif random_integer == 5:

print(“As tough as a creme puff”)

elif random_integer == 6:

print(“LoL!”)

elif random_integer == 7:

print(“Yes…yes you are”)

elif random_integer == 8:

print(“rolls eyes”)

elif random_integer == 9:

print(“Drop and gimme fitty”)

elif random_integer == 10:

print(“crickets”)

else:

default = “Error”

#print(name, “asks:”, question)

#print(“Magic 8-ball’s answer:”, answer)

print(f"{name} asks {question}")

print(f"Magic 8-ball’s answer: {answer}")

Certainly! thank you for helping out!

import random

random_integer = random.randint(1, 10)

# Get user's name
name = input("Enter your name:\n> ")
answer = ''

while True:  # start main loop
    # Get user to ask a question
    question = input("Ask your question:\n> ")
    if random_integer == 1:
        answer = "Naaaah"
    elif random_integer == 2:
        answer = "You eat a bowl of nails w/o any milk?"

    elif random_integer == 3:
        answer = "How many bones haven’t you broken?"

    elif random_integer == 4:
        answer = "Try again…"

    elif random_integer == 5:
        answer = "As tough as a creme puff"

    elif random_integer == 6:
        answer = "LoL!"

    elif random_integer == 7:
        answer = "Yes…yes you are"

    elif random_integer == 8:
        answer = "rolls eyes"

    elif random_integer == 9:
        answer = "Drop and gimme fitty"

    elif random_integer == 10:
        answer = "crickets"

    print(f"{name} asks {question}")
    print(f"Magic 8-ball’s answer: {answer}\n")

Change Log

  • Added a while loop for continuous play
  • Added input() to question to allow user to ask questions.
  • Removed the final else statement
    • Since you’re using randint(1, 10) there will never be an output variable beyond this range

If you have any questions, please do not hesitate to ask!

Best regards,

Yes that did it! It outputs in the order that I want but it only prints answer 5, I’m trying to understand why the code is doing that?

Move:

random_integer = random.randint(1, 10)

With in the While Loop:

while True:  # start main loop
    random_integer = random.randint(1, 10)
    # Get user to ask a question
    question = input("Ask your question:\n> ")

Let me know if that resolves the issue!

You are the best! That worked perfect thank you, i have a lot to learn lol.