如何让我的程序在循环的每次迭代中选择一个新的随机数? -魔术8球

时间:2019-04-24 21:49:16

标签: python-3.x

我正在尝试用python创建一个神奇的8球程序。我让它一次正确地通过了循环,但是每次循环都没有选择一个新的随机数。它不断给出肯定的答案,但这不是最佳的

我认为这可能与我定义函数的方式有关,但是我不知道每次如何调用它。

from time import *
import random


def roll():
    ball = random.randint(1, 20)
    return ball


def isgood(ball):
    if ball >= 10:
        return True
    else:
        return False


def isneutral(ball):
    if 15 >= ball > 10:
        return True
    else:
        return False


def isbad(ball):
    if 20 >= ball > 15:
        return True
    else:
        return False


affirm = ("It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitely.", "You may rely on it.",
          "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes.")

neutral = ("Reply hazy, try again.", "Ask again later.", "Better not tell you now.", "Cannot predict now.",
           "Concentrate and ask again.")

bad = ("Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful.")

shake = 'y'
print("I am a magic 8 Ball")
print("Give me a shake to get your Mystical Answer...")
print("Would you like to shake the 8 ball?")
shake = input('y/n ')
shake = shake.lower()
sleep(0.5)
while shake == 'y':
    if isgood:
        sleep(0.5)
        print(random.choice(affirm))
        print("Shake again?")
        shake = input('y/n ')
        if shake == 'n':
            print("Goodbye!")

    elif isneutral:
        sleep(0.5)
        print(random.choice(neutral))
        print("Shake again?")
        shake = input('y/n ')
        if shake == 'n':
            print("Goodbye!")

    elif isbad:
        sleep(0.5)
        print(random.choice(bad))
        print("Shake again?")
        shake = input('y/n ')
        if shake == 'n':
            print("Goodbye!")

2 个答案:

答案 0 :(得分:0)

您的while循环未调用您的函数。在函数名称后添加括号以调用该函数。那是;不是isgood,而是isgood(last_roll_value)

同样,您似乎并没有在每个循环迭代中调用roll()

尝试在循环顶部添加last_roll_value = roll()。

答案 1 :(得分:0)

您的代码有两点错误:

1。您实际上从未使用 public URLConnection openConnectionOfGivenURL(String givenURL) throws IOException { URL url = new URL(givenURL); return url.openConnection(); } 进行掷骰。通过以下操作来滚动:

getDOM

roll()行之后

2。您实际上从未调用过number = roll() while shake == 'y':isgood()函数。操作方式,基本上是在检查该功能是否存在

通过以下方法执行此操作:

isneutral()

isbad()

,并使用相同的方式调用if isgood: if isgood(number):

编辑:

只想出三分之一。您的isneutral函数正在检查数字是否为isbad,而它应该检查数字isgood