gpt4 book ai didi

python - 程序功能不起作用。它不能传递变量

转载 作者:太空宇宙 更新时间:2023-11-03 21:34:31 26 4
gpt4 key购买 nike

嗯,我想制作一个程序来检查盒子的数量。但我的Python程序无法运行。我用过Python辅导老师,所以,我知道这个原因。function(=def) 不能传递变量。我不知道为什么会出现这个问题...

a = 1

nsave = 100
vsave = 100
msave = 100
csave = 100

nsell = 0
vsell = 0
msell = 0
csell = 0

matrix = [[0]*3 for i in range(4)]
matrix[0][0] = "Nomal"
matrix[0][1] = nsave
matrix[0][2] = nsell
matrix[1][0] = "Vegetable"
matrix[1][1] = vsave
matrix[1][2] = vsell
matrix[2][0] = "Meat"
matrix[2][1] = msave
matrix[2][2] = msell
matrix[3][0] = "Cheese"
matrix[3][1] = csave
matrix[3][2] = csell


def choice(a):
if (a == 1):
nsave = nsave-1
nsell = nsell+1
if (matrix[0][1] < 0):
print("error")
else:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(matrix[i][j])
print()

else:
print("Error")

最佳答案

这是您更正后的代码:

a = 1

nsave = 100
vsave = 100
msave = 100
csave = 100

nsell = 0
vsell = 0
msell = 0
csell = 0

matrix = [[0]*3 for i in range(4)]
matrix[0][0] = "Nomal"
matrix[0][1] = nsave
matrix[0][2] = nsell
matrix[1][0] = "Vegetable"
matrix[1][1] = vsave
matrix[1][2] = vsell
matrix[2][0] = "Meat"
matrix[2][1] = msave
matrix[2][2] = msell
matrix[3][0] = "Cheese"
matrix[3][1] = csave
matrix[3][2] = csell


def choice(a):
global nsave
global nsell

if (a == 1):
nsave = nsave-1
nsell = nsell+1
if (matrix[0][1] < 0):
print("error")
else:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(matrix[i][j])
print()

else:
print("Error")

choice(a)

关于python - 程序功能不起作用。它不能传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53322814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com