我已经查找了有关该问题的其他答案,但我很难在我的程序中实现该解决方案(对于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
答案 0 :(得分:3)
这是一个非常好的开始尝试功能!要解决的两件事:
1)问题变量不应该有印刷品。在input()中放入一个变量将为您打印。这会更好:
question = string+" "+cubed+qmark
2)底部不太正确。也许你想要这样的东西?最后一行再次调用整个函数 - 这称为"递归"。
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()
答案 1 :(得分:1)
这可能会有所帮助:
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
)存储在question
中,然后将question
传递给正在打印它的input
函数< / p>