Il s'agit en quelque sorte d'une copie d'une question que j'ai déjà posée, mais plutôt différente. Je suis un programmeur débutant (jamais programmé auparavant) et on m'a donné une tâche (pour l'école) pour faire un programme qui demande à l'utilisateur une forme (s), puis calculer le volume de cette forme à partir des dimensions données par l'utilisateur. Cependant, si l'utilisateur ne tape pas "Quitter", le programme devrait continuer à demander des formes à l'utilisateur et continuer à calculer les volumes, et lorsque l'utilisateur tape "Quitter", le programme est supposé imprimer la liste des volumes. calculé. les trois formes sont Cube, pyramide et ellipsoïde.Impression d'un message d'erreur si un utilisateur n'a pas entré un certain mot
par exemple, si les types d'utilisateurs dans le cube, cube, pyramide, pyramide, ellipsoïde puis quittez (ainsi que les dimensions nécessaires pour calculer le volume), le programme doit imprimer:
volumes cubiques: 4 , 5
volumes de pyramide: 6, 7
volumes ellipsoïde: 8
REMARQUE: ces chiffres ne sont que par exemple.
J'ai réussi (un peu) à faire remarquer au programme des erreurs et demandé au programme de demander plusieurs fois à l'utilisateur des formes et de calculer des volumes jusqu'à ce que "quit" soit entré. Toutefois, si l'utilisateur ne pénètre pas dans le cube par exemple, la sortie finale devrait être:
volumes cubiques: Vous ne l'avez pas fait de calculs pour le cube
volumes de pyramide: 6, 7
ellipsoïde volumes: 8
au lieu de ce que je reçois en ce moment, ce qui est:
volumes cubiques: []
pyramide v olumes: 6, 7
volumes ellipsoïde: 8
Est-il possible d'atteindre la sortie finale correcte?
Ceci est mon code (il est probablement pas terrible, mais il est le meilleur que je peux faire maintenant en tant que débutant et les choses que nous sommes enseignés à ce jour):
#A program that allows the user to continuously pick different shapes and calculate their volumes.
#import all functions from the math module.
import math
#allows the user to input the shape they want to calculate the volume of. Then that input is converted to all upper case
#letters so that no matter what the user enters, it will be recognizable by the program.
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
#Initializing the lists for the volumes of the three different shapes as empty lists so that it can be filled
#later on.
VolumeListCube = []
VolumeListPyramid =[]
VolumeListEllipsoid = []
#defining the function that will calculate the volume of the cube and then adding them to the empty list VolumeListCube.
def VolumeCube (sideLength):
volume = sideLength**3
#Adding the values to the list
VolumeListCube.append(volume)
#Sorting the values in the created list in ascending order
VolumeListCube.sort()
return;
#defining the function that will calculate the volume of the pyramid and then adding them to the empty list VolumeListPyramid.
def VolumePyramid (baseLength, height):
volume = round((1/3)*(baseLength**2)*height,1)
#Adding the values to the list
VolumeListPyramid.append(volume)
#Sorting the values in the created list in ascending order
VolumeListPyramid.sort()
return;
#defining the function that will calculate the volume of the ellipsoid and then adding them to the empty list VolumeListEllipsoid.
def VolumeEllipsoid (radius1, radius2, radius3):
volume = round((4/3)*math.pi*radius1*radius2*radius3,1)
#Adding the values to the list
VolumeListEllipsoid.append(volume)
#Sorting the values in the created list in ascending order
VolumeListEllipsoid.sort()
return;
#The first while loop here checks if the user immediately inputs "Quit" or not, if they don't, then the next while loop is
#executed, if they do input "Quit", then the program will print the require message.
while shape != "QUIT":
#This is a infinte while loop since true is always true, this allows the error message at the end to be displayed
#and then loop back to the beginning of this loop, so that it can be executed again.
while True:
if shape in ["CUBE","PYRAMID","ELLIPSOID","QUIT"]:
#These if functions will allow the user to input shape parameters depending on what shape the user has chosen, then
#afterwards, another prompt is show for the user to choose another shape and input that shape's parameters. This will
#continue until the user inputs "Quit", then the required message will be printed and the volume results fot all the
#shapes chosen will be displayed in ascending order.
if shape == "CUBE":
sideLength = int(input("Please enter the length of the sides of the cube:"))
#recalling the function that calculates the volume of the cube.
VolumeCube (sideLength)
#lets the user to input another shape they want to calculate the volume of.
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
elif shape == "PYRAMID":
baseLength = int(input("Please enter the base length:"))
height = int(input("Please enter the height:"))
# recalling the function that calculates the volume of the pyramid.
VolumePyramid (baseLength, height)
#lets the user to input another shape they want to calculate the volume of.
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
elif shape == "ELLIPSOID":
radius1 = int(input("Please enter the first radius:"))
radius2 = int(input("Please enter the second radius:"))
radius3 = int(input("Please enter the third radius:"))
# recalling the function that calculates the volume of the ellipsoid.
VolumeEllipsoid (radius1, radius2, radius3)
#lets the user to input another shape they want to calculate the volume of.
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
elif shape == "QUIT":
print ("\nYou have come to the end of the session.\nthe volume calculated for each shape are shown below:\n")
print("The volume of the cube(s) is:", VolumeListCube)
print("The volume of the pyramid(s) is:", VolumeListPyramid)
print("The volume of the ellipsoid(s) is:", VolumeListEllipsoid)
#This exits the program to stop repeated prints that would be caused due to the while loop
exit()
else:
print("Error, please enter a valid shape")
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
else:
print ("\nYou have come to the end of the session.\nYou have not performed any volume calculations")
Une partie de la programmation consiste à pouvoir décomposer la tâche principale en plus petits morceaux qui peuvent être abordés et complétés par eux-mêmes. Je suis submergé par la quantité de code que vous avez présenté et je suppose que vous l'êtes aussi; essayez de réduire le code au strict minimum pour illustrer/tester le problème. J'ai toujours un fichier 'prototype.py' pour tester les petits morceaux. Les concepts standard n'ont pas besoin d'être testés sur votre base de code principale, ce qui rend les choses difficiles. – roganjosh
Merci pour votre commentaire! Je vais essayer de faire ça avec ça et à partir de maintenant. Désolé si j'ai l'air d'un Noob, je viens juste de commencer à coder comme il y a quelques mois et je ne sais qu'un peu. Mais merci pour les conseils ^^ – Pengibaby