Ich habe Code, der das Volumen einer Form bestimmen soll, bestimmt durch Eingabe. Ich habe mehrere if-Anweisungen in einer while-Schleife verschachtelt. Ich möchte, dass die Schleife am Anfang neu startet, wo sie nach Eingabe fragt, aber stattdessen bleibt sie in der Bedingungserklärung hängen. Wenn ich zum Beispiel "cube" eintrage, berechnet es das Volumen und fordert mich dann erneut auf, die Dimensionen einzugeben - stattdessen möchte ich, dass es mir die Form fragt und die if-Anweisungen erneut durchführe. Gleiches am Ende, wenn ich eine Form eingib, die nicht gültig ist, soll es mich bitten, erneut einzugeben, und wenn diese Eingabe gültig ist, fahre entsprechend fort. Aber es geht nicht, es bleibt in einer Endlosschleife hängen und hört nie auf, mich zu bitten, eine Form einzugeben.Python - Machen Sie, wenn Anweisung in While-Schleifen die Schleife neu starten, nicht die Bedingung?
import math
import decimal
shape = str(input("Please enter a shape: "))
shape = shape.lower()
shapes = ["cube", "pyramid", "ellipsoid", "quit"]
stored_cube_vol = []
stored_pymd_vol = []
stored_ellip_vol = []
while shape != "quit":
if shape == "cube":
def cube_volume(cube_side_length):
cube_total_vol = decimal.Decimal(cube_side_length**3)
return round(cube_total_vol, 2)
cube_side_length = float(input("Enter the length of length of one side of the cube: "))
print("The volume of a cube with sides with a length of", cube_side_length, "is", cube_volume(cube_side_length))
stored_cube_vol.append(cube_volume(cube_side_length))
elif shape == "pyramid":
def pymd_volume(pymd_base, pymd_height):
pymd_total_vol = decimal.Decimal(pymd_base**2)*pymd_height*(1/3)
return round(pymd_total_vol, 2)
pymd_base = float(input("Enter the length of the base of the pyramid: "))
pymd_height = float(input("Enter the height of the pyramid: "))
print("The volume of a pyramid with a base of", pymd_base, "and a height of", pymd_height, "is", pymd_volume(pymd_base, pymd_height))
stored_pymd_vol.append(pymd_volume(pymd_base, pymd_height))
elif shape == "ellipsoid":
def ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3):
ellip_total_vol = decimal.Decimal(math.pi*(4/3)*ellip_rad_1*ellip_rad_2*ellip_rad_3)
return round(ellip_total_vol, 2)
ellip_rad_1 = float(input("Enter the first radius: "))
ellip_rad_2 = float(input("Enter the second radius: "))
ellip_rad_3 = float(input("Enter the third radius: "))
print("The volume of an ellipsoid with radii", ellip_rad_1, ellip_rad_2, "and", ellip_rad_3, "is", ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3))
stored_ellip_vol.append(ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3))
elif shape == "quit":
print("You have come to the end of the session.")
print("The volumes calculated for each shape are shown below.")
print("Cube: ", sorted(stored_cube_vol))
print("Pyramid: ", sorted(stored_pymd_vol))
print("Ellipsoid: ", sorted(stored_ellip_vol))
else:
str(input("Please enter a shape: ").lower())
Warum sind Sie Funktionen innerhalb Ihrer Schleife definieren? –
weil ich nicht weiß, was ich mache lol .. kann ich all diese vor der while loop beginnen? – DR2016
Sie möchten shape = input ... am Anfang der while-Schleife aufrufen. Im Moment nach der Würfelberechnung ist die Form = Würfel noch so, also geht es zurück in die Schleife. – Chris