Python if语句不工作?
我刚刚开始学习python,通过《像计算机科学家一样思考》一书,我陷入了一些语言语法中Python if语句不工作?,python,if-statement,conditional-statements,Python,If Statement,Conditional Statements,我刚刚开始学习python,通过《像计算机科学家一样思考》一书,我陷入了一些语言语法中 def amt(): amount = input("Enter your amount: ") amount = int(amount) if amount >= 20: twe = amount / 20 amount = amount - twe * 20 print("You need to pay %d twent
def amt():
amount = input("Enter your amount: ")
amount = int(amount)
if amount >= 20:
twe = amount / 20
amount = amount - twe * 20
print("You need to pay %d twenty dollar bills" %(twe))
if amount >= 10:
ten = amount / 10
amount = amount - ten * 10
print("You need to pay %d ten dollar bills" %(ten))
if amount >= 5:
five = amount / 5
amount = amount - five * 5
print("You need to pay %d five dollar bills" %(five))
if amount >= 1:
one = amount / 1
amount = amount - one * 1
print("You need to pay %d one dollar bills" %(one))
amt()
当我使用一些输入(比如7)运行此程序时,会收到如下错误消息:
Traceback (most recent call last):
File "dollars.py", line 21, in <module>
amt()
File "dollars.py", line 7, in amt
print("You need to pay %d twenty dollar bills" %(twe))
UnboundLocalError: local variable 'twe' referenced before assignment
回溯(最近一次呼叫最后一次):
文件“dollars.py”,第21行,在
金额()
文件“dollars.py”,第7行,金额
打印(“您需要支付%d张20美元的钞票”%(twe))
UnboundLocalError:赋值前引用的局部变量“twe”
为什么if语句不能正常工作?即使输入值小于20,它仍然会进入第一个if语句中,这在视觉上并不明显,但您正在混合制表符和空格。看起来print语句在
if
中,但实际上不是
这是您的源代码在Stack Overflow的文本编辑器中的外观:
它在视觉上不明显,但你在混合标签和空格。看起来print语句在
if
中,但实际上不是
这是您的源代码在Stack Overflow的文本编辑器中的外观:
复制和粘贴您的代码,它运行时不会出错,但它不会做您想要的事情。我想你把
%
(模数)和/
(整数除法)混在一起,以确定你需要的账单数量
此外,当您在代码中看到大量重复的行时,您可能需要重新组织代码。基本上,您的amt
归结为:
def amt():
amount = int(input('Enter your amount: ') )
for name, nomination in [ ('twenty', 20), ('ten', 10), ('five', 5), ('one', 1) ]:
bills = amount // nomination
amount -= bills * nomination
if not bills: continue
print('You need to pay {} {} dollar bill{}.'.format (bills, name, '' if bills == 1 else 's') )
关于整数除法的几个词:
对于所有
a∈ ℤ代码>和b∈ ℤ \ {0}
,正好存在一个d∈ ℤ代码>和正好一个r∈ ℤ代码>,这样a=db+r
和0≤ r
。根据定义a//b=d
和a%b=r
复制和粘贴您的代码时,它运行时不会出错,但不会执行您想要的操作。我想你把%
(模数)和/
(整数除法)混在一起,以确定你需要的账单数量
此外,当您在代码中看到大量重复的行时,您可能需要重新组织代码。基本上,您的amt
归结为:
def amt():
amount = int(input('Enter your amount: ') )
for name, nomination in [ ('twenty', 20), ('ten', 10), ('five', 5), ('one', 1) ]:
bills = amount // nomination
amount -= bills * nomination
if not bills: continue
print('You need to pay {} {} dollar bill{}.'.format (bills, name, '' if bills == 1 else 's') )
关于整数除法的几个词:
对于所有a∈ ℤ代码>和b∈ ℤ \ {0}
,正好存在一个d∈ ℤ代码>和正好一个r∈ ℤ代码>,这样a=db+r
和0≤ r
。根据定义a//b=d
和a%b=r
给定的7
作为输入,此代码工作正常。这看起来是正确的。你绝对确定这就是正在运行的代码吗?运行前是否保存了文件?由于代码看起来不错,我认为您查看的文件与正在执行的文件和/或未保存的文件不同,或者存在空白问题。试着把你的帖子中的代码复制到一个新文件中,然后运行它。是的,我肯定就是这样。我已将该文件保存为$s.py,并使用python$s.py编译它,它要求输入,但当我输入7时,我会收到错误消息。您的新输出是由于使用了%
而不是/
。请看我的答案。给定7
作为输入,此代码工作正常。这看起来正确。你绝对确定这就是正在运行的代码吗?运行前是否保存了文件?由于代码看起来不错,我认为您查看的文件与正在执行的文件和/或未保存的文件不同,或者存在空白问题。试着把你的帖子中的代码复制到一个新文件中,然后运行它。是的,我肯定就是这样。我已将该文件保存为$s.py,并使用python$s.py编译它,它要求输入,但当我输入7时,我会收到错误消息。您的新输出是由于使用了%
而不是/
。请看我的答案。OP可以运行python-tt his_program_name.py
来确认。当我们都看到它不是缩进时,为什么你坚持它是完美缩进的呢?是的。OP可以运行python-tt his_program_name.py
来确认。为什么我们都可以看到它不是完全缩进的呢?请注意,复制和粘贴代码会丢失缩进错误,而缩进错误实际上会生成OP报告的错误消息。您已经修复了代码逻辑。请注意,复制和粘贴代码会丢失缩进错误,而缩进错误实际上是生成OP报告的错误消息。您已经修复了代码逻辑。