Python Forum
What am i doing wrong - math tool box - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: What am i doing wrong - math tool box (/thread-9611.html)



What am i doing wrong - math tool box - TJOm - Apr-18-2018

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



RE: What am i doing wrong - math tool box - woooee - Apr-18-2018

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.1/handsonHtml/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


RE: What am i doing wrong - math tool box - nilamo - Apr-18-2018

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)).


RE: What am i doing wrong - math tool box - TJOm - Apr-18-2018

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.


RE: What am i doing wrong - math tool box - nilamo - Apr-18-2018

And? What output do you get from printing it?


RE: What am i doing wrong - math tool box - TJOm - Apr-18-2018

 
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?




RE: What am i doing wrong - math tool box - nilamo - Apr-18-2018

(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).


RE: What am i doing wrong - math tool box - TJOm - Apr-18-2018

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'
>>>


RE: What am i doing wrong - math tool box - nilamo - Apr-18-2018

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'



RE: What am i doing wrong - math tool box - vishalhule - Jun-24-2018

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.