首次尝试 Magic 8 Ball 程序,不循环

First attempt at Magic 8 Ball program, not looping

这是我第一次尝试制作一个像魔术 8 球一样的程序,但是我似乎无法让程序再次循环,也就是说,我无法让程序询问用户在输入 'Y' 后输入另一个问题。如何让程序在第一个问题得到回答后要求用户在键入 'Y' 后输入另一个问题?

# 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): ')

除了关于再次提问的评论,这是你的问题:

while keep_going == 'Y':

当您键入 y 以继续时,y 是小写的,而不是大写的 Y,您应该更改它,这样不管是小写还是大写都没有关系。

试试这个:

#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): ')