我在定义语句时遇到更多问题
I'm having more issues with defining statements
我正在尝试用输入循环做一个定义语句。如果输入在语句之外,我唯一的问题是它重复输入两次,这有点奇怪。仅在 while 循环内输入,它会给出 amt not defined 的错误。
def Loan_Amount(amt):
while True:
amt = int(input("Please put in the loan amount you would like to take out: "))
if amt < 500:
print("Sorry, we don't offer loans below 500 dollars")
continue
elif amt >= 500:
break
print(Loan_Amount(amt))
另一种看起来很奇怪的方式
amt = int(input('Please put in the loan amount you would like to take out: '))
def Loan_Amount(amt):
while True:
amt = int(input("Please put in the loan amount you would like to take out: "))
if amt < 500:
print("Sorry, we don't offer loans below 500 dollars")
continue
elif amt >= 500:
break
print(Loan_Amount(amt))
好的,我把它留了下来,以防其他人遇到这个问题,但在查看文档后我想通了。
def Loan_Amount(prompt)
While True:
global amt
amt = int(input(prompt))
if amt < 500:
print("Sorry, we don't offer loans below 500 dollars")
continue
else:
break
Loan_Amount('Please put in the loan amount you would like to take out: ')
print(amt)
我不是 100% 确定我是否要正确解释这一点,所以请随意理解我的解释。基本上而不是像 def Loan_Amount(amt) 这样的括号内有 amt 导致它既是参数又是全局变量而不是提示并且不是在循环内键入输入而是在调用时键入根据声明。 global amt 部分在那里我很确定,所以当 amt 被赋予一个数字时,它将使 amt 成为一个全局变量,而不是将它保留在循环内的本地,这样你就可以在循环外打印它,它仍然是一个定义的变量。希望这能帮助像我一样迷茫的人!
我正在尝试用输入循环做一个定义语句。如果输入在语句之外,我唯一的问题是它重复输入两次,这有点奇怪。仅在 while 循环内输入,它会给出 amt not defined 的错误。
def Loan_Amount(amt):
while True:
amt = int(input("Please put in the loan amount you would like to take out: "))
if amt < 500:
print("Sorry, we don't offer loans below 500 dollars")
continue
elif amt >= 500:
break
print(Loan_Amount(amt))
另一种看起来很奇怪的方式
amt = int(input('Please put in the loan amount you would like to take out: '))
def Loan_Amount(amt):
while True:
amt = int(input("Please put in the loan amount you would like to take out: "))
if amt < 500:
print("Sorry, we don't offer loans below 500 dollars")
continue
elif amt >= 500:
break
print(Loan_Amount(amt))
好的,我把它留了下来,以防其他人遇到这个问题,但在查看文档后我想通了。
def Loan_Amount(prompt)
While True:
global amt
amt = int(input(prompt))
if amt < 500:
print("Sorry, we don't offer loans below 500 dollars")
continue
else:
break
Loan_Amount('Please put in the loan amount you would like to take out: ')
print(amt)
我不是 100% 确定我是否要正确解释这一点,所以请随意理解我的解释。基本上而不是像 def Loan_Amount(amt) 这样的括号内有 amt 导致它既是参数又是全局变量而不是提示并且不是在循环内键入输入而是在调用时键入根据声明。 global amt 部分在那里我很确定,所以当 amt 被赋予一个数字时,它将使 amt 成为一个全局变量,而不是将它保留在循环内的本地,这样你就可以在循环外打印它,它仍然是一个定义的变量。希望这能帮助像我一样迷茫的人!