Zuerst ein paar Dinge:
Der Unterschied zwischen "input" und "raw_input" besteht darin, dass die Eingabe einen Zahlenwert zurückgibt, während raw_input einen Zeichenfolgenwert zurückgibt. Alles, was du mit raw_input bekommst, muss in ein int oder float umgewandelt werden, wenn du vorhast, es in einer numerischen Operation zu verwenden. Ebenso müssen Sie raw_input verwenden, wenn Sie erwarten, dass der Benutzer ein Wort oder einen Ausdruck eingibt.
Zweitens platziert die Druckfunktion die Ausgabe automatisch auf einer neuen Zeile. Wenn Sie Ihrer Ausgabe eine neue Zeile hinzufügen möchten, verwenden Sie '\ n' in Ihrer Zeichenfolge.
Schließlich, Gussradius zu einem Schwimmer sichergestellt, dass es als ein Schwimmer ausgedruckt werden würde. Wenn Sie es nicht in einen Fließkommabereich umwandeln, bevor Sie es in einen String umwandeln, übernimmt python die Formatierung für Sie.
Im Folgenden habe ich Ihren Code, kommentiert aus der fehlerhaften Teile und legte meine Korrekturen direkt unter ihnen:
print("Do you want to find the area of a circle? ")
# again = input("Enter 'y' for yes, or enter 'n' to exit the program: ")
again = raw_input("Enter 'y' for yes, or enter 'n' to exit the program: ")
while (again == 'y'):
pi = 3.14
# radius = raw_input(" Input the radius of the circle: ")
radius = input("Input the radius of the circle: ")
area = pi * radius * radius
# print("A circle with a radius of " + str(float(radius)) + " has an area of " + "{0:.2f}".format(area))
print("A circle with a radius of " + str(radius) + " has an area of " + "{0:.2f}".format(area))
# print()
# print("Would you like to run another? ")
print("Would you like to run another? ")
# again = input("Enter 'y' to run another, enter 'n' to exit: ")
again = raw_input("Enter 'y' to run another, enter 'n' to exit: ")
print("Have a nice day :) ")
hoffe, das hilft!
verwenden, anstatt für Python 3:
print("Do you want to find the area of a circle?")
again = input("Enter 'y' for yes, or enter 'n' to exit the program: ")
while (again == 'y'):
pi = 3.14
radius = input("Input the radius of the circle: ")
area = pi * (float(radius) ** 2)
print("A circle with a radius of " + str(radius) + " has an area of " + "{0:.2f}".format(area))
print("Would you like to run another?")
again = input("Enter 'y' to run another, enter 'n' to exit: ")
print("Have a nice day :)")
Nutzungsart (Radius). – roadrunner66