這是我之前問過的一個問題的副本,但有點不同。如果用戶沒有輸入某個單詞,則輸出錯誤信息
我是一個編程初學者(從來沒有編程過),我已經給了一個任務(學校)來製作一個程序,要求用戶提供一個形狀,然後根據給定的尺寸計算該形狀的體積用戶。但如果用戶沒有輸入「quit」,程序應該不斷詢問用戶形狀並繼續計算卷,並且當用戶輸入「quit」時,程序將假設打印出卷的列表計算。三個形狀是立方體,金字塔和橢圓體。
,例如,如果在立方體,立方體,棱錐,棱錐形,橢圓體,然後在用戶鍵入退出(與必要的尺寸沿着計算體積),則程序應該打印出:
立方體體積:4 ,5
金字塔卷:6,7
橢球體積:8
注:這些數字只是例如。
我已經成功(有點)讓程序注意到錯誤,並讓程序重複詢問用戶形狀和計算體積,直到輸入「quit」。但是,如果用戶沒有例如進入立方體,那麼最終的輸出應該是:
立方體體積:你有沒有做過任何計算多維數據集
金字塔卷:6,7
橢球卷:8
什麼,而不是我現在得到,這就是:
立方體的體積:[]
金字塔v olumes:6,7
橢圓形卷:8
有什麼辦法來實現正確的最終輸出?
這是我的代碼(這可能不是很大,但它是最好的,我現在可以做一個初學者,用那東西我們至今教授):
#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")
部分是能夠打破下來的主要任務分成小塊,可以解決並自行完成。我對你提供的代碼量感到不知所措,我想你也是。嘗試將代碼降到最低限度以說明/測試問題。我總是有一個'prototype.py'文件來測試小塊。標準概念不需要在主代碼庫中進行測試,這使事情變得艱難。 – roganjosh
謝謝您的評論!我會盡力做到這一點,從現在開始。對不起,如果我看起來像一個noob,我只是剛剛開始編碼像一個月前,只知道一點。不過謝謝你的提示^^ – Pengibaby