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

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Simple answer, just new to programming


    Hey everyone,

    I just started learning python (first time trying to pick up a programming language) and after reading through some of the basics I've decided to write a simple program that will be a 'math quiz' just to reiterate what I've learned thus far.

    I want the program to ask a series of progressively harder questions with some basic feedback from the program in the form of text. I'm only a few lines in and have run into a problem that I'm sure has a simple solution, I just can't figure it out with my limited knowledge on the subject hah. Heres my code:

    n = raw_input("Hello, enter your name to begin: ")
    print "Nice to meet you " + n

    a = input("Now enter your age: ")
    if a >= 12:
    print "This test should be a breeze then!"
    else:
    print "Well put your thinking cap on, this might get tough!"

    b = input("Lets start with something easy, 5x3= ")
    if b == 15:
    print "Good work!"
    else:
    print "Oops, try again!"

    Now I want the 'b' input to repeat itself if the answer they typed in is wrong but I'm not sure how to go about doing that. so far everything works fine, but if you type in the answer wrong you have to restart the program from the beginning instead of just printing "Oops, try again!" and repeating the question until they get it right. Any tips or hints will be greatly appreciated!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    b=0
    while b!=15:
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0
    Thanks for the tip man! Did a little research on the wiki python pages and was able to come up with this:
    Code:
    while True:
        b = input("Lets start with something easy, 5x3= ")
        if b == 15:
            print "Good job!"
            break
        else:
            print "Oops, try again!"
    Works like a charm! Now I just need to figure out how to implement my code into a simple GUI hah. Thanks again
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    I'm glad it's working for you and I know it's standard practice for Python (and maybe other languages) but I find the "while True" structure inelegant at best and scary at worst. You're relying on a "break" to get out of a loop with an "if" statement that seems completely superfluous. It just seems better to me to loop with the condition being that which you are checking anyway.

    Just my (antiquated) perspective. I'm sure I'll get flamed royally now.

IMN logo majestic logo threadwatch logo seochat tools logo