Python 3.5:猜立方根游戏返回 None 作为输出

Python 3.5: Guessing the cube root game is returning None as an output

我已经查找了有关该问题的其他答案,但我很难在我的程序中实施该解决方案(对 Python 来说仍然很新)。这是我目前所拥有的:

# Purpose: 'guess' randomly selects a number between 0 and 100 (including those
# numbers) and calculate the cube of that number. It also prompts the user to 
# guess the cube root of the cubed number, displaying 'Correct!' if correct, and
# asking the user if they would like to try again if incorrect.

def guess():
    import random
    rand = random.randint (0, 100)
    cubed = rand*rand*rand

    # Forming the input question
    string = "What is the cube root of"
    cubed = str(cubed)
    qmark = "?"
    question = print(string, cubed + qmark)

    user = int(input(question))
    while (user == rand):
        print ("\nCorrect!")
        user = input("\n\tWould you like to try again?").lower()


    if (user != rand):
        print ("\tIncorrect!, the cube root of", cubed, "is", rand)

这应该是输出的样子:

guess()

What is the cube root of 64000? 56
            Incorrect, the cube root of 64000 is 40

            Would you like to try again? y

What is the cube root of 216? 6
        Correct!

        Would you like to try again? n

Goodbye

这是一个很好的功能开始尝试!要解决的 2 件事:

1) question 变量中不应包含打印内容。将变量放入 input() 将为您打印。这样会更好:

question = string+" "+cubed+qmark

2) 底部部分不太正确。也许你想要这样的东西?最后一行再次调用整个函数 - 这称为 "recursion".

user = int(input(question))
if (user == rand):
    print ("\nCorrect!")
elif (user != rand):
    print ("\tIncorrect!, the cube root of", cubed, "is", rand)
user = input("\n\tWould you like to try again?").lower()
if user == "y":
    guess()

放在一起,这是最终代码:

# Purpose: 'guess' randomly selects a number between 0 and 100 (including those
# numbers) and calculate the cube of that number. It also prompts the user to 
# guess the cube root of the cubed number, displaying 'Correct!' if correct, and
# asking the user if they would like to try again if incorrect.

def guess():
    import random
    rand = random.randint (0, 100)
    cubed = rand*rand*rand

    # Forming the input question
    string = "What is the cube root of"
    cubed = str(cubed)
    qmark = "?"
    question = string+" "+cubed+qmark #################### Remove print here. Input will do it for you.

    user = int(input(question))
    if (user == rand):
        print ("\nCorrect!")
    elif (user != rand):
        print ("\tIncorrect!, the cube root of", cubed, "is", rand)
    user = input("\n\tWould you like to try again?").lower()
    if user == "y":
        guess()
    else:
        print("Goodbye")

guess()

我自己也是最近才开始编程,帮助别人还是第一次!

编辑:根据评论中的建议,这里有一种不使用递归的方法,将所有内容放入 "While True Loop",也称为永远循环:

def guess():
    while True:
        import random
        rand = random.randint (0, 100)
        cubed = rand*rand*rand

        # Forming the input question
        string = "What is the cube root of"
        cubed = str(cubed)
        qmark = "?"
        question = string+" "+cubed+qmark #################### Remove print here. Input will do it for you.

        user = int(input(question))
        if (user == rand):
            print ("\nCorrect!")
        elif (user != rand):
            print ("\tIncorrect!, the cube root of", cubed, "is", rand)
        user = input("\n\tWould you like to try again?").lower()
        if user != "y":
            print("Goodbye")
            break

guess()

这可能有帮助:

def guess():
    import random
    rand = random.randint (0, 100)
    cubed = rand*rand*rand

    # Forming the input question
    question = "What is the cube root of {cubed} ?".format(cubed=cubed)

    user = int(input(question))
    if user == rand:
        print ("\nCorrect!")

    else:
        print ("\tIncorrect!, the cube root of", cubed, "is", rand)

choice = 'y'
while choice == 'y':
  guess()
  choice = input("Would you like to play again ?").lower()
else:
  print("Goodbye")

您将 print 函数(即 None)的 return 值存储在 question 中,然后将 question 传递给 input打印它的函数