Jython output help (beginner question)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • od11kd
    New Member
    • Feb 2013
    • 2

    Jython output help (beginner question)

    Hey guys!

    So I am trying to do a program, and for the most part I have it down except for 1 part of it.

    Basically, I want my function to continue to prompt a user to enter an integer number between 1 and 100 inclusive, until the user enters a invalid integer.

    I have the program here:
    Code:
    def assign2PartB(): 
    x = 0 
    while x <= 100: 
    x = input ("Enter a number between 1 and 100 to continue: ") 
    printNow ("The number you entered was: " + str(x)) 
    if x <=0: 
    printNow ("The number you entered (" + str(x) + ") was not between 1 and 100. Try again, please!")
    And the output is as followed:
    >>> assign2PartB()
    Enter a number between 1 and 100 to continue: 19
    The number you entered was: 19
    Enter a number between 1 and 100 to continue: 99
    The number you entered was: 99
    Enter a number between 1 and 100 to continue: 0
    The number you entered was: 0
    The number you entered (0) was not between 1 and 100. Try again, please!

    My question is, when I enter "0" how do I get my program to not show "The number you entered was: 0" but only show "The number you entered (0) was not between 1 and 100. Try again, please!"

    Thanks so much in advance guys, I appreciate your help!
    Last edited by Niheel; Feb 5 '13, 06:24 AM. Reason: Please don't delete questions when someone has taken time to help
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You need to put "printNow ("The number you entered was: " + str(x))" in an if/elif/else block. Example:
    Code:
    def assign2PartB(): 
        while True: 
            x = input("Enter a number between 1 and 100 to continue:") 
            if x < 1: 
                print "The number you entered (%s) was not between 1 and 100. Try again, please!" % (x)
            elif x > 100:
                print "The number you entered was: %s" % (x)
                print "Exiting!"
                return
            else:
                print "The number you entered was: %s" % (x)
    
    assign2PartB()

    Comment

    • od11kd
      New Member
      • Feb 2013
      • 2

      #3
      Thank you so much! Appreciate it

      Comment

      Working...