双重问题; Python 3.9中通过函数调用范围内的随机整数
Twofold Question; Calling a random integer in a range through a function in Python 3.9
这确实是一个双重问题,因为我知道同一对代码块有两个错误。
我基本上是在制作一个基于“滚动 d20”而变化的交互式故事。例如,玩家会看到一个场景,并被提示掷 d20。计算机生成一个 1 到 20 之间的数字,根据掷骰结果,故事会以某种方式展开。我遇到的障碍是我定义了一个函数“RollD20()”,它将变量“n”的值存储为 1 到 20 之间的随机整数,然后打印滚动的值。当我调用该函数时,它崩溃并显示错误消息“n is not defined”。
这个问题的另一部分,在同一个代码块中,是我试图让游戏从本质上询问用户,你想玩吗?如果答案是肯定的,那么剩下的就结束了。如果不是,则过程结束。但到目前为止,无论我按什么键,y 或 yes,n 或 no,甚至 enter,它都不会像预期的那样结束过程,它只是向前移动。有解决这个问题的简单方法吗?
谢谢,代码如下。
import sys
import random
while True:
def NewGame():
print(input("Hello! Would you like to go on an adventure? y/n >> "))
if input == "y" or "yes":
print("Great! Roll the dice.")
print(input("Press R to roll the D20."))
print("You rolled a " + RollD20(n) + "!")
else:
print(input("Okay, bye! Press any key to exit."))
sys.exit()
def RollD20():
n = random.randint(1, 20)
print(n)
NewGame()
Traceback (most recent call last):
\venv\main.py", line 22, in <module>
NewGame()
\venv\main.py", line 11, in NewGame
print("You rolled a " + RollD20(n) + "!")
NameError: name 'n' is not defined
Process finished with exit code 1
好的,你的代码有很多错误,所以我会逐一检查。
您需要将输入分配给一个变量,以便您可以在 if 语句中进行比较。你永远不应该在现有的 Python 函数或对象之后命名变量,所以我将它命名为 inp
.
真的没有必要打印每一个输入语句;只需调用 input
.
而不是 x == "y" or "yes"
,您需要 x == "y" or x == "yes"
。
对于 RollD20
函数,您应该 return 而不是打印 n
它,因为您在 NewGame
函数。
在NewGame
函数中,不需要向RollD20
传递任何参数。此外,由于它 return 是一个整数,您必须将结果转换为字符串才能打印它。
话虽如此,下面是完整的更正代码:
import sys
import random
while True:
def NewGame():
inp = input("Hello! Would you like to go on an adventure? y/n >> ")
if inp == "y" or inp == "yes":
print("Great! Roll the dice.")
input("Press R to roll the D20.")
print("You rolled a " + str(RollD20()) + "!")
else:
input("Okay, bye! Press any key to exit.")
sys.exit()
def RollD20():
n = random.randint(1, 20)
return n
NewGame()
这确实是一个双重问题,因为我知道同一对代码块有两个错误。
我基本上是在制作一个基于“滚动 d20”而变化的交互式故事。例如,玩家会看到一个场景,并被提示掷 d20。计算机生成一个 1 到 20 之间的数字,根据掷骰结果,故事会以某种方式展开。我遇到的障碍是我定义了一个函数“RollD20()”,它将变量“n”的值存储为 1 到 20 之间的随机整数,然后打印滚动的值。当我调用该函数时,它崩溃并显示错误消息“n is not defined”。
这个问题的另一部分,在同一个代码块中,是我试图让游戏从本质上询问用户,你想玩吗?如果答案是肯定的,那么剩下的就结束了。如果不是,则过程结束。但到目前为止,无论我按什么键,y 或 yes,n 或 no,甚至 enter,它都不会像预期的那样结束过程,它只是向前移动。有解决这个问题的简单方法吗?
谢谢,代码如下。
import sys
import random
while True:
def NewGame():
print(input("Hello! Would you like to go on an adventure? y/n >> "))
if input == "y" or "yes":
print("Great! Roll the dice.")
print(input("Press R to roll the D20."))
print("You rolled a " + RollD20(n) + "!")
else:
print(input("Okay, bye! Press any key to exit."))
sys.exit()
def RollD20():
n = random.randint(1, 20)
print(n)
NewGame()
Traceback (most recent call last):
\venv\main.py", line 22, in <module>
NewGame()
\venv\main.py", line 11, in NewGame
print("You rolled a " + RollD20(n) + "!")
NameError: name 'n' is not defined
Process finished with exit code 1
好的,你的代码有很多错误,所以我会逐一检查。
您需要将输入分配给一个变量,以便您可以在 if 语句中进行比较。你永远不应该在现有的 Python 函数或对象之后命名变量,所以我将它命名为
inp
.真的没有必要打印每一个输入语句;只需调用
input
.而不是
x == "y" or "yes"
,您需要x == "y" or x == "yes"
。对于
RollD20
函数,您应该 return 而不是打印n
它,因为您在NewGame
函数。在
NewGame
函数中,不需要向RollD20
传递任何参数。此外,由于它 return 是一个整数,您必须将结果转换为字符串才能打印它。
话虽如此,下面是完整的更正代码:
import sys
import random
while True:
def NewGame():
inp = input("Hello! Would you like to go on an adventure? y/n >> ")
if inp == "y" or inp == "yes":
print("Great! Roll the dice.")
input("Press R to roll the D20.")
print("You rolled a " + str(RollD20()) + "!")
else:
input("Okay, bye! Press any key to exit.")
sys.exit()
def RollD20():
n = random.randint(1, 20)
return n
NewGame()