我的代码需要询问用户3个号码。如果数字超过100
或1
,请告诉他们"no way, try a different number"
我的问题是:我无法弄清楚如何定义我的变量prompt
,并且在运行我的代码时获取下面的stacktrace
。名称“提示”未定义
代码:
def get_int(prompt, minval, maxval):
"""gets a value for an input. if its too small or large gives error"""
n= int(input("Choose a number between 1 and 100: "))
maxval= n > 100
minval= n< 1
prompt = n
int_choice.append(n)
return None
int_choice=[]# list for adding inputs
for i in range (3):
get_int(prompt, minval, maxval)
if n== minval or n== maxval:
print("no way, try a diffrent number")
int_choice.append(n)
print("you chose: ", int_choice)
堆栈跟踪:
>line 18, in <module>
get_int(prompt, minval, maxval)
NameError: name 'prompt' is not defined
is the error message
在调用get_int之前,将prompt提示为promt =“”(在for循环中的get_int(提示符,最小值,最大值)之前) – labheshr
您希望'prompt'具有什么值?你打算让你的代码做什么? – BrenBarn
不只是提示值被破坏,所有的脚本都在使用外部的get_int变量 – AndreL