What is wrong with this python code (magic 8 ball game)

python

#1

D4rk_S0rc3ry

import random

ans1 = "The only way you'll ever get that answer from me is to pry it from my cold, dead hands."
ans2 = "Thou shalt be exalted with the gnarliest of fist bunps."
ans3 = "Thou'rt mad to say it."
ans4 = "How dare you ask me that!"
ans5 = "That information is classified."
ans6 = "If I told you, I'd have to kill you."
ans7 = "HAHA sucks to be you."
ans8 = "I find your visage highly displeasing."
ans9 = "Only you can save mankind."
ans10 = "The price of my fortunes...is the blood of a freshly exsanguinated gopher."
ans11 = "= _ ="
ans12 = "Fear of the unknown is what imprisons us."
ans13 = "Don't look behind you."
ans14 = "Don't blink."
ans15 = "STOP THIS NONSENSE"
ans16 = "You offend me deeply."
ans17 = "I find our conversations tedious."
ans18 = "Whatever."
ans19 = "Dumbass."
ans20 = "This is the way the world ends: not with a bang but with a whimper."


print("hi, welcome to the useless magic 8 ball! Please hold.")
print("\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...")
print("(we will not provide you with any legitimate means to discern future outcomes.)\n how may we be of service today?")


question = input("input query. Press ENTER to shake. if you dare. can you handle the COLD, HARD, TRUTH???\n")

print("shaking..../n" *5 ,"loading...\n" *4 , "consulting psychiatrists for professional responses" , "\n BINGO! you got lucky! your life is about to change.")



choice=random.randint(1, 20)

if choice == 1:
    answer=ans1

elif choice == 2:
    answer=ans2
    
elif choice == 3:
    answer=ans3
    
elif choice == 4:
    answer=ans4
    
elif choice == 5:
     answer=ans5
    
elif choice == 6:
    answer=ans6
    
elif choice == 7:
    answer=ans7
    
elif choice == 8:
    answer=ans8
    
elif choice == 9:
    answer=ans9
    
elif choice == 10:
    answer=ans10
    
elif choice == 11:
    answer=ans11
    
elif choice == 12:
    answer=ans12
    
elif choice == 13:
    answer=ans13
    
elif choice == 14:
    answer=ans14
    
elif choice == 15:
    answer=ans15
    
elif choice == 16:
    answer=ans16
    
elif choice == 17:
    answer=ans17
    
elif choice == 18:
    answer=ans18
    
elif choice == 19:
    answer=ans19
    
else:
    answer=ans20
  

while question != "quit":

    print(answer)
    print("ask again, or type *quit*")
    question = input()

#2

Can you add a bit of context, what is the output or the error you get? Try using three backticks (`) before and after your code to make it formatted.


#3

every time i run it, the answer is the same for every question

when i kill it and rerun it, a different answer shows but is still the same for each question


#4

The issue is, you loop over three things - printing the answer, ask again or type quit and then getting an input from the user. You should also try adding the generation of random answers to the while loop (the choice flow)

Currently, when your code runs choice gets set to a random number. Then answer gets set to corresponding answer and they never get changed.


#5

ok, but how do i fix it?


#6

Get the choice=random line and the whole if/elif structure below it and put it inside the while loop


#7

I would do something like this

from random import randint
from time import sleep as sl

responses = [
    "Welcome to the useless magic 8 ball! Please hold.",
    "\n...",
    "(we will not provide you with any legitimate means to discern future outcomes.)",
    "\nHow may we be of service today?",
    "If you would like to shake the ball just press enter, otherwise enter quit.\n",
    "consulting psychiatrists for professional responses",
    "\nBINGO! you got lucky! your life is about to change.",
    "shaking....\n",
    "loading..."
]

answers = [
    "The only way you'll ever get that answer from me is to pry it from my cold, dead hands.",
    "Thou shalt be exalted with the gnarliest of fist bunps.",
    "Thou'rt mad to say it.",
    "That information is classified.",
    "If I told you, I'd have to kill you.",
    "HAHA sucks to be you.",
    "I find your visage highly displeasing.",
    "Only you can save mankind.",
    "The price of my fortunes...is the blood of a freshly exsanguinated gopher.",
    "= _ =",
    "Fear of the unknown is what imprisons us.",
    "Don't look behind you.",
    "Don't blink.",
    "STOP THIS NONSENSE",
    "You offend me deeply.",
    "I find our conversations tedious.",
    "Whatever.",
    "Dumbass.",
    "This is the way the world ends: not with a bang but with a whimper."
]


def print_count_wait(msg, count=1, time_wait=1):
    while count > 0:
        print(msg)
        sl(time_wait)
        count -= 1
    sl(time_wait)


def give_result():
    """
    This function returns a value from the answers list.
    :return:
    """
    return answers[randint(0, len(answers) - 1)]


def shake_ball(s_count=5, l_count=3):
    while True:
        hold = input(responses[4])
        if hold == 'quit':
            break
        print_count_wait(responses[7], s_count)
        print_count_wait(responses[8], l_count)
        print(responses[5], responses[6])
        print(give_result())


if __name__ == "__main__":
    print(responses[0])
    print_count_wait(responses[1], 5)
    print(responses[2], responses[3])
    shake_ball()

I added some wait timers to the code so it makes it appear as if it is working, I also changed your answers into a list so you could just use random on the list and get it that way.

Also, I like holding all text in a list in one place that way I can easily modify any of the text entries in one place. Makes it easier to maintain a program.

I also created a custom print function that prints out a message a certain amount of times after waiting a given period of time.

If you have any questions ask freely.

@shadowmar

That is a good suggestion also.