Python Forum
What am i doing wrong - math tool box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What am i doing wrong - math tool box
#1
So I was told to create a math tool box which I have but for some reason when I actually enter a value nothing happens and or my code tells me not valid choice try again. I do not need help with the assignment as I am already almost done BUT I don't get why this is happening. Also this is my first post hopefully I'm following all the rules.

ans=True
while ans:
    print("""
    1.Square
    2.Rectangle
    3.Triangle
    4.Circle
    5.Exit the program
    """)
    ans=input("What would you like to do?")
    if ans==1:
        print("""Square is selected.""")
        side = input("Now input side")
        print ("Area is"),side*side
        print ("Circumference is"),4*side
    elif ans==2:
        print("Rectangle is selected")
        length = input("Now input length")
        breadth = input("Now input breadth")
        print ("Area is"),length*breadth
        print ("Circumference is"),2*(length+breadth)
    elif ans==3:
        print("Triangle is selected")
        side1 = input("Now input side 1")
        side2 = input("Now input side 2")
        side3 = input("Now input side 3")
        height = input("Now input height ")
        base = input("Now input base")
        print ("Area is"),0.5*base*height
        print ("Circumference is"),(side1+side2+side3)
    elif ans==4:
        print("Circle is selected")
        radius = input("Now input radius")
        print ("Area is"),3.14*radius*radius
        print ("Circumference is"),(2*3.14*radius)


    elif ans==5:
        print("Goodbye")
        ans = None
    else:
        print("Not valid choice try again")
Reply
#2
First, you have to indent your code properly for it to work. Then see "Numbers and Strings of Digits" at https://anh.cs.luc.edu/python/hands-on/3...ml/io.html Finally look at at least one tutorial. Tutorials are write once read many so volunteers on forums don't have to waste their time answering the same question over and over https://wiki.python.org/moin/BeginnersGuide/Programmers
Reply
#3
In python 2.x, you should be using raw_input(), not input(). But that wouldn't cause what you're seeing (it's just something you should do).

Let's take a look at what you're typing. Right after your ans=input("What would you like to do?"), add this: print(repr(ans)).
Reply
#4
Thanks mod but I added print(repr(ans)) right below ans=input("What would you like to do?") But when I do enter a number that is correct I still get number is not valid.
Reply
#5
And? What output do you get from printing it?
Reply
#6
 
1.Square
2.Rectangle
3.Triangle
4.Circle
5.Exit the program

What would you like to do?1
'1'
Not valid choice try again

1.Square
2.Rectangle
3.Triangle
4.Circle
5.Exit the program

What would you like to do?

Reply
#7
(Apr-18-2018, 07:45 PM)TJOm Wrote: What would you like to do?1
'1'


Boom, there's your answer. See the quotes? ans is a string. You're comparing it against ints, which will never be true. So, either...

a) change your if statements to compare against a string: if ans == "1":, or
b) force ans to be an int: ans = int(ans).
Reply
#8
okay thanks it worked now the only I'm dealing with is a traceback error




1.Square
2.Rectangle
3.Triangle
4.Circle
5.Exit the program

What would you like to do?1
'1'
Square is selected.
Now input side1
Area is
Traceback (most recent call last):
File "C:/Users/tunji/Desktop/projecttwo.py", line 15, in <module>
print ("Area is"),side*side
TypeError: can't multiply sequence by non-int of type 'str'
>>>
Reply
#9
That's the same error:
>>> 4 * 4
16
>>> "4" * "4"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
Reply
#10
do indentation properly

ans=True
while ans:
    print("""
    1.Square
    2.Rectangle
    3.Triangle
    4.Circle
    5.Exit the program
    """)
    ans=input("What would you like to do?")
    ans = ans.strip()
    if ans=='1':
        print("""Square is selected.""")
        side = int (input("Now input side"))
        print ("Area is", side*side)
        print ("Circumference is",4*side)
    
you can do for other cases also
by defalultinput() function converts userinput into string.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020