为什么未定义 a2 的错误代码显示

why is an error code for a2 not being defined show

find numbers and compares with eachother

to see if they are bigger or smaller.

x = input("Your First Capacity? ")
y = input("Your Second Capacity? ")
z = input("Your Required Capacity? ")

x = int(x)
y = int(y)
z = int(z)

if x <= z:
    if y != z:
        if x != z:
            a1 = x
            b1 = y

if y <= z:
    if x != z:
        if y != z:
            a1 = y
            b1 = x

if one number is the same the code dosnt carry on

if (x == z) or (y == z):
    print("Required Capasity Already Reached")


a1 = 0
b1 = 0

statements for making a2 = the remaining of a1 for 0-10.

if a1 == 0:
    a2 = a1

statements for making b2 = the remaining of b1 from 0-10.

if b1 == 0:
    b2 = b1

a1 = a1 - 1
b1 = b1 - 1

如果 a1 = 3

,为什么 print(a2) 返回 0 而不是 1

提前致谢。

这会导致您出错,因为您使用 if 语句将输入与字符串进行比较,而您已经将它们(您的输入)转换为整数。您应该改用

if a1 == 1:  # Notice no quotation marks around the number. Important!!
   a2 = a1 - 1

因为您使用过:

x = int(x)
y = int(y)
z = int(z)

您需要为所有 if 语句更改此项。

祝你好运!