How do you add an entry into a 'raw_input' string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuugie
    New Member
    • Sep 2010
    • 32

    How do you add an entry into a 'raw_input' string?

    I am currently having trouble trying to make the entry typed in show up in the next message that should pop up. The person is suppose to type in two numbers, the second one being larger than the first. After the two numbers are entered, I'm trying to get it to say "Enter a number from (first number entered) to (second number entered): " but i keep getting an error.

    This is my code:
    Code:
    num1 = int(raw_input('Enter a small number: '))
    num2 = int(raw_input('Enter a large number: '))
    print num1
    print num2
    
    entry = 0                                          # not valid
    while not num1 <= entry <= num2:
      try:
        entry = int(raw_input('Enter a number from', num1, 'to', num2,': '))
        if not num1 <= entry <= num2:
          print 'Your number must be from  to .'
      except ValueError:
        print 'That is not a valid integer.'    # error message
    
    raw_input('Press Enter to continue!')
    Any help would be appreciated
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    String formatting.
    Code:
    num1 = int(raw_input('Enter a small number: '))
    num2 = int(raw_input('Enter a large number: '))
    print num1
    print num2
     
    entry = 0                                          # not valid
    while not num1 <= entry <= num2:
      try:
        entry = int(raw_input("Enter a number from %s to %s:" % (num1, num2)))
        if not num1 <= entry <= num2:
          print 'Your number must be from %s to %s.' % (num1, num2)
      except ValueError:
        print 'That is not a valid integer.'    # error message
     
    raw_input('Press Enter to continue!')

    Comment

    • Fuugie
      New Member
      • Sep 2010
      • 32

      #3
      Alright thanks and if you wouldn't mind helping me with one more thing. I'm having some trouble getting my error message to work correctly.

      Code:
      num1 = int(raw_input('Enter a small number: '))     # First Number
      num2 = int(raw_input('Enter a larger number: '))     # Second Number
      
      if not num2 >= num1:
            print 'The number must be larger than the first...'  # Error message
            
      entry = 0                                          
      while not num1 <= entry <= num2:
        try:
          entry = int(raw_input('Enter a number from %s to %s: ' % (num1, num2)))
          if not num1 <= entry <= num2:
            print 'Your number must be from %s to %s.' % (num1, num2)
        except ValueError:
          print 'That is not a valid integer.'    # error message
      
      raw_input('Press Enter to continue!')
      I'm trying to get it to say, "The number must be larger than the first..." and have it make you re-enter the number, but instead it makes the message pop up and then say "Enter a number from (num1) to (num2):

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        That can be done with a while loop:
        Code:
        >>> while True:
        ...     num1 = int(raw_input('Enter a small number: '))
        ...     num2 = int(raw_input('Enter a larger number: '))
        ...     if not num2 >= num1:
        ...         print 'The number must be larger than the first...'
        ...     else:
        ...         break
        ...     
        The number must be larger than the first...
        >>>

        Comment

        Working...