2016-04-17 10 views
0

Ich bin ein Anfänger in Python 3. Ich versuche, eine Ausgabe als Integer oder Float je nach verwendetem Typ zu drucken. Wie kann ich den Code so formatieren, dass er bei Eingabe von 3 als Radius "3" ausgibt und bei Eingabe von 3,5 den Radius als "3,5" ausgibt? Gibt es irgendwelche Vorschläge?Wie kann ich eine Ganzzahl ausgeben und float abhängig von der Eingabe in Python?

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 = raw_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() 
    print("Would you like to run another? ") 
    again = input("Enter 'y' to run another, enter 'n' to exit: ") 

print("Have a nice day :) ") 
+0

Nutzungsart (Radius). – roadrunner66

Antwort

0

für debug oder einfach nur die Art von Wert zu wissen, dass Sie

print(type(some_var)) 

verwenden können, wenn Sie sind es in verwendet werden soll if-Anweisung Verwendung mit isinstance Funktion

if isinstance(radius, int): 
    print("int") 
if isinstance(radius, float): 
    print("float") 

weitere Infos hier Differences between isinstance() and type() in python

0

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 :)") 
+0

Danke für die Hilfe! Ich habe überall gesucht und ich konnte es nicht herausfinden, ich werde meinen Code entsprechend anpassen. Danke nochmal! –

+0

Kein Problem! Viel Glück! – Kaeon

+0

Wenn ich versucht habe mit Ihrem Code habe ich das als Fehler >>> circleArea() Wollen Sie die Fläche eines Kreises finden? Traceback (jüngste Aufforderung zuletzt): File "" Linie 1 in kreisFlaeche() File "/Users/shor269/Desktop/Python-Horn/Geometry.py", Zeile 49, in kreisFlaeche wieder = raw_input ("Gib 'y' für yes ein, oder gib 'n' ein, um das Programm zu verlassen:") NameError: Name 'raw_input' ist nicht definiert –

Verwandte Themen