Results 1 to 9 of 9

Thread: Python beginner help plz

  1. #1
    Join Date
    Apr 2007
    Location
    U.S
    Beans
    389
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Python beginner help plz

    I just started Python and im reading a how to ( http://hetland.org/writing/instant-hacking.html ) and im trying to get this coding to work and it seems to be working fine except it wont let me put in 1 or 2 to chose wat i want circle or triangle wat the coding does is calculate the area of a circle or triangle y cant i get it to take the input and calculet it heres the coding
    print "welcome"
    print "chose 1=tri 2=cir"
    shape=input(">")
    if shape == 1:
    height = input()
    width = input()
    area = input()
    print "area=", area
    else:
    radius = input()
    area = 3.14*(radius**2)
    print "area2=", area

    Im using SPE but i tried it in DrPython and IDLE
    please help i really want to proggram

    Another ? - the mark how do i get to Pygame i already installed it but its not under proggraming or any thing please help

  2. #2
    Join Date
    Dec 2005
    Beans
    217
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Python beginner help plz

    hey, i forget the EXACT syntax, but its something like : shape = input("Choose 1 for triangle, 2 for circle")
    Also, python is based off of indentation, so make sure u have your if and else stuff indented. Anytime you want to get input just pick a name of the input, like radius, and make it equal to input("What is the radius").

    Code:
    print "welcome"
    shape = input("choose 1=tri 2=cir")
    if shape == 1:
       height = input("height: ")
       width = input("width: ")
       area = input("area: ")
       print "area=", area
    else:
       radius = input("radius: ")
       area = 3.14*(radius**2)
       print "area2=", area
    I don't have python installed here, so I can't test it out... but try that

  3. #3
    Join Date
    Sep 2005
    Location
    Cedar Rapids, IA, USA
    Beans
    545
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Python beginner help plz

    Using [ code ] [ /code ] (without the spaces) will make your code easier to read in a post. It might also help if you used proper English and not shortened words, it'd be easier to understand what you are trying to ask.

    Here is my version of what you typed and it works (with the exception of the second input for the triangle)
    Code:
    print "welcome"
    print "choose 1=tri 2=cir"
    
    shape=input(">")
    if shape == 1:
            height=input("h:")
            base=input("b:")
            area = (1/2.)*base*height
    else:
            radius=input("r")
            area = 3.14*radius*radius
    
    print "area = ", area
    Last edited by xtacocorex; May 11th, 2007 at 03:34 AM.
    #399784 | Ubuntu User #287
    *** If you're going to program, install the damn build-essential package ***
    There is no such thing as Ubuntu specific programming
    Save the electrons - if you quote, trim!

  4. #4
    Join Date
    Apr 2007
    Location
    U.S
    Beans
    389
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Python beginner help plz

    its saying print welcome choose 1=tri 2=cir but it wont let me put in 1 or 2. y not?

  5. #5
    Join Date
    Dec 2005
    Beans
    217
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Python beginner help plz

    did u try my code above??? cause just printing to the screen wont let u input values

  6. #6
    Join Date
    Dec 2005
    Beans
    217
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Python beginner help plz

    oops.... lol take out this line area = input("area: ") lol you shouldn't ask for the area, thats what the program is suppose to to lol... and I tested it and it works...

    Code:
    print "Welcome"
    shape = input("choose 1=triangle or 2=circle: ")
    if shape == 1:
       height = input("height: ")
       width = input("width: ")
       print "area=", area
    else:
       radius = input("radius: ")
       area = 3.14*(radius**2)
       print "area2=", area

  7. #7
    Join Date
    Jan 2007
    Beans
    43
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Python beginner help plz

    Code:
    shape = raw_input("choose 1=triangle, 2=circle")
    if shape == "1":
        height = int(raw_input("Height:"))
        width = int(raw_input("Width:"))
        area =  height * width /2
        print "area=", area
    elif shape == "2":           #I did this in case the user entered 3 or higher
        radius = int(raw_input("Enter radius"))
        area = 3.14*(radius**2)
        print "area2=", area

  8. #8
    Join Date
    Jan 2006
    Location
    @g0st
    Beans
    92
    Distro
    Ubuntu 6.06 Dapper

    Re: Python beginner help plz

    By the way i suggest you use raw_input and cast it as an integer type versus using input. A malicious user can enter a command via input.
    "Do you have any gold pieces? I want to use all your gold pieces...
    AT THE GAY TAVERN ! GAY TAVERN!"
    -- Dover McPunhcie, Maleficus Smythe, and Cornellius Dastardly
    (This is why i love D&D)

  9. #9
    Join Date
    Dec 2005
    Beans
    217
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Python beginner help plz

    awesome thx for the tip!!! I haven't used python in almost a year

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •