#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Newbie programming on python


    ###What am I doing wrong? This is a math quiz that gives 2 random numbers to add.
    # main function
    def main():
    print("This program prints 2 random numbers between 1 and 2000 and asks you to add them.")
    print("Type the answer after you see the 2 random numbers.")
    #generate 2 different numbers to add together
    num1 = random.randint(1,2000)
    num2 = random.randint(1,2000)
    # print and add the two integers
    # correct answer to equation
    total = sum(num1, num2)
    # print the equation on 3 lines
    print(num1, \n, "+",\n, num2))
    # asking for the user to input answer
    answer = int(input("Enter answer to math problem: "))
    # if statement
    if total == answer
    print("You are correct!)
    else:
    print("Try again." \n, "Correct answer is: " \n, format(num3, '.2f'))
    # defining the addition function
    def sum(num1, num2):
    return num1 + num2

    # call main function
    main()
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    Well, first off, your posting in the wrong forum - there's a separate forum for Python, if you scroll up to the first "Programming Languages" group. But that's beside the point.

    Not so beside the point is that you need to put your code samples in [code] or [highlight] tags, so that the indentation is preserved. This is particularly important for Python, where the indentation and formatting are actually significant parts of the program structure. You can either enter the tags manually, or you can do it by pressing the "#" or the highlight marker buttons at the top of the editing window.
    python Code:
    import random
     
    # main function
    def main():
        print("This program prints 2 random numbers between 1 and 2000 and asks you to add them.")
        print("Type the answer after you see the 2 random numbers.")
        #generate 2 different numbers to add together
        num1 = random.randint(1,2000)
        num2 = random.randint(1,2000)
        # print and add the two integers
        # correct answer to equation
        total = sum(num1, num2)
        # print the equation on 3 lines
        print(num1, \n, "+",\n, num2))
        # asking for the user to input answer
        answer = int(input("Enter answer to math problem: "))
        # if statement
        if total == answer:
            print("You are correct!")
        else:
            print("Try again." \n, "Correct answer is: " \n, format(num3, '.2f'))
     
    # defining the addition function 
    def sum(num1, num2):
        return num1 + num2
     
    # call main function    
    main()

    As for what is wrong, part of it is that the indentation was off in places, which would cause a nesting error. Next, you omitted the import randomstatement at the beginning of the program. Also, you dropped the colon in the if: statement, which makes for another syntax error. Finally, you dropped a double-quote ending one of the strings in a print call. I have fixed these issues in the code above.

    There may be other problems that aren't so obvious; we would need you to tell us what is going wrong, what error messages are coming up, and so forth. If the program fails or crashes, post the traceback. If it runs but is incorrect, tell us about the incorrect behavior. We can just look at a program and blindliy guess what is happening when it is run.
    Last edited by Schol-R-LEA; June 1st, 2013 at 03:31 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    8
    Rep Power
    0
    thank you- i forgot that my tabs/indents did not roll over when I transferred this file. i will try the python forum next time. thanks for help.

IMN logo majestic logo threadwatch logo seochat tools logo