为什么 elif 语句不起作用? [Python]
Why is the elif statement not working? [Python]
我想让代码计算由 1 到 5 之间的数字选择的物体的体积。例如,如果你 运行 代码并按 3,它应该计算骰子的体积.
问题是无论我输入哪个数字,我每次都会得到 else 条件。当我看到 elif 示例时,它们只有打印功能,我是不是使用了 if-elif 错误?
我尝试更改输入类型、更改运算符并尝试四处移动,但我不知道哪里出了问题。
x = print(int(input("Which body do u want to calculate?:\n [1] Sphere \n [2] Cylinder \n [3] Dice \n [4] Quadar \n [5] Pyramid \n ")))
if x == 1:
r = int(input("Give the radius in (cm): "))
sphere_volume = (4/3) * pi * r ** 3
print("The volume of your body is: " + str(sphere_volume) + " cm^3")
elif x == 2:
r = int(input("Give the radius in (cm): "))
h = int(input("Give the height in (cm): "))
cylinder_volume = pi * sqrt(r) * h
print("The volume of your body is: " + str(cylinder_volume) + " cm^3")
elif x == 3:
k = float(input("Give the edge lenght in (cm): "))
dice_volume = k ** 3
print("The volume of your body is: " + str(dice_volume) + " cm^3")
elif x == 4:
k = float(input("Give the edge lenght in (cm): "))
a = float(input("Give the depth in (cm): "))
h = float(input("Give the height in (cm): "))
quadar_volume = k * a * h
print("The volume of your body is: " + str(quadar_volume) + " cm^3")
elif x == 5:
k = float(input("Give the edge length in (cm): "))
h = float(input("Give the height in (cm): "))
pyramid_volume = 1/3 * sqrt(k) * h
print("The volume of your body is: " + str(pyramid_volume) + " cm^3")
else:
print("Wrong entry. Choose a number between 1 and 5")```
错误在代码的第一行,如果你把它改成这样就可以了。感谢Demi-Lune
x = int(input("Welchen Körper möchten Sie berechenn:\n [1] Kugel \n [2] Zylinder \n [3] Würfel \n [4] Quadar \n [5] Pyramide \n "))
x = print(int(input("Which body do u want to calculate?:\n [1] Sphere \n [2] Cylinder \n [3] Dice \n [4] Quadar \n [5] Pyramid \n ")))
您正在将 x
设置为任何 print()
returns,但是该函数 returns None
.
>>> x = print("test")
test
>>> type(x)
<class 'NoneType'>
只需将行更改为:
x = int(input("Which body do u want to calculate?:\n [1] Sphere \n [2] Cylinder \n [3] Dice \n [4] Quadar \n [5] Pyramid \n "))
重构为
x = int(input("Which body do u want to calculate?:\n [1] Sphere \n [2] Cylinder \n [3] Dice \n [4] Quadar \n [5] Pyramid \n "))
print(x) # 1 or 2 or 3..
因为总是打印 return None
word = "anything"
p = print(word) # "anything"
print(p) # None
我想让代码计算由 1 到 5 之间的数字选择的物体的体积。例如,如果你 运行 代码并按 3,它应该计算骰子的体积.
问题是无论我输入哪个数字,我每次都会得到 else 条件。当我看到 elif 示例时,它们只有打印功能,我是不是使用了 if-elif 错误?
我尝试更改输入类型、更改运算符并尝试四处移动,但我不知道哪里出了问题。
x = print(int(input("Which body do u want to calculate?:\n [1] Sphere \n [2] Cylinder \n [3] Dice \n [4] Quadar \n [5] Pyramid \n ")))
if x == 1:
r = int(input("Give the radius in (cm): "))
sphere_volume = (4/3) * pi * r ** 3
print("The volume of your body is: " + str(sphere_volume) + " cm^3")
elif x == 2:
r = int(input("Give the radius in (cm): "))
h = int(input("Give the height in (cm): "))
cylinder_volume = pi * sqrt(r) * h
print("The volume of your body is: " + str(cylinder_volume) + " cm^3")
elif x == 3:
k = float(input("Give the edge lenght in (cm): "))
dice_volume = k ** 3
print("The volume of your body is: " + str(dice_volume) + " cm^3")
elif x == 4:
k = float(input("Give the edge lenght in (cm): "))
a = float(input("Give the depth in (cm): "))
h = float(input("Give the height in (cm): "))
quadar_volume = k * a * h
print("The volume of your body is: " + str(quadar_volume) + " cm^3")
elif x == 5:
k = float(input("Give the edge length in (cm): "))
h = float(input("Give the height in (cm): "))
pyramid_volume = 1/3 * sqrt(k) * h
print("The volume of your body is: " + str(pyramid_volume) + " cm^3")
else:
print("Wrong entry. Choose a number between 1 and 5")```
错误在代码的第一行,如果你把它改成这样就可以了。感谢Demi-Lune
x = int(input("Welchen Körper möchten Sie berechenn:\n [1] Kugel \n [2] Zylinder \n [3] Würfel \n [4] Quadar \n [5] Pyramide \n "))
x = print(int(input("Which body do u want to calculate?:\n [1] Sphere \n [2] Cylinder \n [3] Dice \n [4] Quadar \n [5] Pyramid \n ")))
您正在将 x
设置为任何 print()
returns,但是该函数 returns None
.
>>> x = print("test")
test
>>> type(x)
<class 'NoneType'>
只需将行更改为:
x = int(input("Which body do u want to calculate?:\n [1] Sphere \n [2] Cylinder \n [3] Dice \n [4] Quadar \n [5] Pyramid \n "))
重构为
x = int(input("Which body do u want to calculate?:\n [1] Sphere \n [2] Cylinder \n [3] Dice \n [4] Quadar \n [5] Pyramid \n "))
print(x) # 1 or 2 or 3..
因为总是打印 return None
word = "anything"
p = print(word) # "anything"
print(p) # None