为什么不能在函数下覆盖变量?
Why can't a variable be overwritten under a function?
a = int(input("How many pizza would you like to order? ")) #line 1
def count_ordered_pizza():
if a==0:
print("Okay see u next time!")
elif a==1:
print(str(a)+ " pizza ordered.")
elif a>1:
print(str(a)+ " pizzas ordered.")
else:
print("Sorry that's impossible")
def reorder():
user_said= str(input('Do you wish to order again? State Y/N. '))
while user_said != "N":
a = int(input("How many pizza would you like to order? ")) #line 16
count_ordered_pizza()
reorder()
print('Bye!')
如果我执行此操作,reorder()
函数(第 16 行)中的 a
不会替换 count_ordered_pizza()
函数(第 1 行)中的 a
。这是为什么?
我认为它可以替换它们,因为变量已被覆盖,但如果我这样做:
def reorder():
user_said= str(input('Do you wish to order again? State Y/N. '))
while user_said != "N":
a = int(input("How many pizza would you like to order? ")) #line 16
if a==0:
print("Okay see u next time!")
elif a==1:
print(str(a)+ " pizza ordered.")
elif a>1:
print(str(a)+ " pizzas ordered.")
else:
print("Sorry that's impossible")
reorder()
print('Bye!')
它显示了正确的值。有人可以帮我吗?提前致谢!
如果在函数 global explanation
中使用 global a
表达式,则可以更改全局变量值
a = int(input("How many pizza would you like to order? ")) #line 1
def count_ordered_pizza():
if a==0:
print("Okay see u next time!")
elif a==1:
print(str(a)+ " pizza ordered.")
elif a>1:
print(str(a)+ " pizzas ordered.")
else:
print("Sorry that's impossible")
def reorder():
user_said= str(input('Do you wish to order again? State Y/N. '))
while user_said != "N":
global a
a = int(input("How many pizza would you like to order? ")) #line 16
count_ordered_pizza()
reorder()
print('Bye!')
a = int(input("How many pizza would you like to order? ")) #line 1
def count_ordered_pizza():
if a==0:
print("Okay see u next time!")
elif a==1:
print(str(a)+ " pizza ordered.")
elif a>1:
print(str(a)+ " pizzas ordered.")
else:
print("Sorry that's impossible")
def reorder():
user_said= str(input('Do you wish to order again? State Y/N. '))
while user_said != "N":
a = int(input("How many pizza would you like to order? ")) #line 16
count_ordered_pizza()
reorder()
print('Bye!')
如果我执行此操作,reorder()
函数(第 16 行)中的 a
不会替换 count_ordered_pizza()
函数(第 1 行)中的 a
。这是为什么?
我认为它可以替换它们,因为变量已被覆盖,但如果我这样做:
def reorder():
user_said= str(input('Do you wish to order again? State Y/N. '))
while user_said != "N":
a = int(input("How many pizza would you like to order? ")) #line 16
if a==0:
print("Okay see u next time!")
elif a==1:
print(str(a)+ " pizza ordered.")
elif a>1:
print(str(a)+ " pizzas ordered.")
else:
print("Sorry that's impossible")
reorder()
print('Bye!')
它显示了正确的值。有人可以帮我吗?提前致谢!
如果在函数 global explanation
中使用global a
表达式,则可以更改全局变量值
a = int(input("How many pizza would you like to order? ")) #line 1
def count_ordered_pizza():
if a==0:
print("Okay see u next time!")
elif a==1:
print(str(a)+ " pizza ordered.")
elif a>1:
print(str(a)+ " pizzas ordered.")
else:
print("Sorry that's impossible")
def reorder():
user_said= str(input('Do you wish to order again? State Y/N. '))
while user_said != "N":
global a
a = int(input("How many pizza would you like to order? ")) #line 16
count_ordered_pizza()
reorder()
print('Bye!')