0

The Problem has been solved. However, my account is new, so it won't let me answer the question. It seems that changing the instances of input() with raw_input() makes it work. I honestly have no clue why, but it may have something to do with the differences between 2 and 3.

I'm supposed to make a program that can calculate area and circumference. That part hasn't been so difficult.

However, the menu isn't working. The first part of the menu works just fine, but after you make a selection, it doesn't print what it's supposed to.

import math

print ("""
1) Calculate circumference
2) Calculate area
""")
ans = input("Enter 1, 2, or 0 to exit this program: ")
if ans == "1":
    diameter = float(input("Enter the diameter: "))
    if diameter > 0:
        circumference = (diameter*math.pi)
        print("The circumference is", circumference)
    else:
        print("Error: the diameter must be a positive number.")
if ans == "2":
    radius = float(input("Enter the radius: "))
    if radius > 0:
        area = ((radius**2)*math.pi)
        print("The area is", area)
    else:
        print("Error: the radius must be a postive number.")
if ans == "0":
    print("Thanks for hanging out with me!")
    quit()

4 Answers4

1

After indenting properly, change if ans == "1" to str(ans) == "1" or ans == 1 and it should be fine.

This should work:

import math

print ("""
1) Calculate circumference
2) Calculate area
""")
ans = input("Enter 1, 2, or 0 to exit this program: ")
if str(ans) == "1":
    diameter = input("Enter the diameter: ")
    print diameter
    if float(diameter) > 0.0:
        circumference = (diameter*math.pi)
        print("The circumference is", circumference)
    else:
        print("Error: the diameter must be a positive number.")
....

PS: As mentionned in the comments, it works, but it is disgusting. We should only use ans == 1 or modify input to input_raw if you use python < python 3

Jeremy D
  • 4,787
  • 1
  • 31
  • 38
0

Your code's indentation shows the span of control of each 'if', 'for', or 'while'. You need to further indent the instructions under each major 'if' statement so that they only get executed when that 'if' statement is selected. Otherwise, everything that is at the leftmost indentation gets executed every time through the loop. For example, you should have something like:

if answer == '1':
....<statements that execute under condition 1>
elif answer == '2':
....<statements that execute under condition 2>

where I have emphasized the indentation with '....'.

mikeTronix
  • 584
  • 9
  • 16
0

The second if statement is not within the brackets of the first. this is true for both 1 and 2.

if ans == "1";
    diameter = float(input("enter the diameter: "))
    if diameter ....
anon
  • 1
0

for me treating ans as integer worked. What I mean by treating ans as integer is instead of writing ans=="1" ,I wrote ans==1.it print corret cvalue. Check this code:


import math

print ("""
1) Calculate circumference
2) Calculate area
""")
ans = input("Enter 1, 2, or 0 to exit this program: ")
print ans
if ans ==1:
    diameter = float(input("Enter the diameter: "))
    if diameter > 0:
        circumference = (diameter*math.pi)
        print("The circumference is", circumference)
    else:
        print("Error: the diameter must be a positive number.")
if ans == 2:
    radius = float(input("Enter the radius: "))
    if radius > 0:
        area = ((radius**2)*math.pi)
        print("The area is", area)
    else:
        print("Error: the radius must be a postive number.")
if ans == 0:
    print("Thanks for hanging out with me!")
    quit()