0

I am a beginner in Python 3. I am trying to be able to print an output as either an integer or a float depending on which type is used. How am I able to format the code so that if they enter 3 as the radius it outputs "3" and if they were to input 3.5 it would output the radius as "3.5"? Are there any suggestions?

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 :) ")
Ren
  • 2,852
  • 2
  • 23
  • 45

2 Answers2

0

for debuging or just to know the type of value you can use

print(type(some_var))

if are you want to use it in if statement use with isinstance function

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

more info here Differences between isinstance() and type() in python

Community
  • 1
  • 1
someone
  • 31
  • 5
0

First, a few things:

The difference between "input" and "raw_input" is that input will return a number value whereas raw_input will return a string value. Anything you get with raw_input will have to be cast to an int or a float if you intend to use it in a numerical operation. likewise, you have to use raw_input if you expect the user to enter a word or phrase.

Second, the print funtion places the output on a new line automatically. if you want to add another new line to your output, use '\n' in your string.

Finally, casting radius to a float ensured it would print out as a float. if you don't cast it to a float before casting it to a string, python will do the formatting for you.

Below I took your code, commented out the flawed parts, and placed my fixes directly below them:

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

Hope this helps!

Use this instead for 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 :)")
Kaeon
  • 11
  • 4
  • Thanks for the help! I was looking everywhere and I was unable to figure it out, I will adjust my code accordingly. Thank you again! – BicsGaming _ Apr 17 '16 at 15:41
  • No problem! Good luck! – Kaeon Apr 18 '16 at 00:53
  • When i tried using your code I got this as an error >>> circleArea() Do you want to find the area of a circle? Traceback (most recent call last): File "", line 1, in circleArea() File "/Users/shor269/Desktop/Python-Horn/Geometry.py", line 49, in circleArea again = raw_input("Enter 'y' for yes, or enter 'n' to exit the program: ") NameError: name 'raw_input' is not defined – BicsGaming _ Apr 18 '16 at 16:32
  • My bad. The advice I gave for input and raw_input only applies to python 2. In python 3 "input" returns a string, which you then have to cast. Also, in python you can make exponents by using **. For example: print(10 ** 2) would print out 100. I'll add a fixed version of the code to the answer. Sorry about that. – Kaeon Apr 19 '16 at 05:03
  • It worked out for me when I ran it! Also, it is fine I was in no rush, I was really stuck on this and you helped me out so thanks again! – BicsGaming _ Apr 19 '16 at 16:33