Issue
This is my first attempt at making a program that acts like a magic 8 ball, however I can't seem to get the program to loop again, that is, I can't get the program to ask the user to input another question after typing in a 'Y'. How do I get the program to ask the user to input another question after typing a 'Y' once their first question is answered?
# All the possible Magic 8 Ball responses
response = ["As I see it, yes", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Don't count on it", "It is certain", "It is decidedly so", "Most likely", "My reply is no", "My sources say no", "Outlook not so good", "Outlook good", "Reply hazy, try again", "Signs point to yes", "Very doubtful", "Without a doubt", "Yes", "Yes - definitely", "You may rely on it"]
import math
#C onstants
N = 10000 # The norm
A = 4875 # The adder
M = 8601 # The multiplier
X = input("Enter a YES or NO question: ")
S = int(input("Now enter an integer: "))
K = 1
print("The Magic 8 Ball says:")
# -----------------------------------------
# The pseudorandom number generator
keep_going = 'Y'
while keep_going == 'Y':
for i in range(K):
S = (S * M + A) % N # Random Number Generator
r = S/N #On the interval [0,1)
magic = math.floor(20 *r)
print(response[magic])
#Asking the user if they want to ask another question
keep_going = input('Do you want to ask another question ' +
'question? (Enter Y for yes and N for no): ')
Solution
Try this:
#All the possible Magic 8 Ball responses
response = ["As I see it, yes", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Don't count on it", "It is certain", "It is decidedly so", "Most likely", "My reply is no", "My sources say no", "Outlook not so good", "Outlook good", "Reply hazy, try again", "Signs point to yes", "Very doubtful", "Without a doubt", "Yes", "Yes - definitely", "You may rely on it"]
import math
#Constants
N = 10000 #The norm
A = 4875 #The adder
M = 8601 #The multiplier
K = 1
print("The Magic 8 Ball says:")
#-----------------------------------------
#The pseudorandom number generator
keep_going = 'Y'
while keep_going in ['y', 'Y']: # allow for lower case 'y'
X = input("Enter a YES or NO question: ") # moved question into loop
S = int(input("Now enter an integer: "))
for i in range(K):
S = (S * M + A) % N #Random Number Generator
r = S/N #On the interval [0,1)
magic = math.floor(20 *r)
print(response[magic])
#Asking the user if they want to ask another question
keep_going = input('Do you want to ask another question ' +
'question? (Enter Y for yes and N for no): ')
Answered By - Erik Gomersbach
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.