By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

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

P: 32
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:
Expand|Select|Wrap|Line Numbers
  1. num1 = int(raw_input('Enter a small number: '))
  2. num2 = int(raw_input('Enter a large number: '))
  3. print num1
  4. print num2
  5.  
  6. entry = 0                                          # not valid
  7. while not num1 <= entry <= num2:
  8.   try:
  9.     entry = int(raw_input('Enter a number from', num1, 'to', num2,': '))
  10.     if not num1 <= entry <= num2:
  11.       print 'Your number must be from  to .'
  12.   except ValueError:
  13.     print 'That is not a valid integer.'    # error message
  14.  
  15. raw_input('Press Enter to continue!')
  16.  
Any help would be appreciated
Sep 30 '10 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
String formatting.
Expand|Select|Wrap|Line Numbers
  1. num1 = int(raw_input('Enter a small number: '))
  2. num2 = int(raw_input('Enter a large number: '))
  3. print num1
  4. print num2
  5.  
  6. entry = 0                                          # not valid
  7. while not num1 <= entry <= num2:
  8.   try:
  9.     entry = int(raw_input("Enter a number from %s to %s:" % (num1, num2)))
  10.     if not num1 <= entry <= num2:
  11.       print 'Your number must be from %s to %s.' % (num1, num2)
  12.   except ValueError:
  13.     print 'That is not a valid integer.'    # error message
  14.  
  15. raw_input('Press Enter to continue!')
  16.  
Oct 1 '10 #2

P: 32
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.

Expand|Select|Wrap|Line Numbers
  1. num1 = int(raw_input('Enter a small number: '))     # First Number
  2. num2 = int(raw_input('Enter a larger number: '))     # Second Number
  3.  
  4. if not num2 >= num1:
  5.       print 'The number must be larger than the first...'  # Error message
  6.  
  7. entry = 0                                          
  8. while not num1 <= entry <= num2:
  9.   try:
  10.     entry = int(raw_input('Enter a number from %s to %s: ' % (num1, num2)))
  11.     if not num1 <= entry <= num2:
  12.       print 'Your number must be from %s to %s.' % (num1, num2)
  13.   except ValueError:
  14.     print 'That is not a valid integer.'    # error message
  15.  
  16. raw_input('Press Enter to continue!')
  17.  
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):
Oct 1 '10 #3

bvdet
Expert Mod 2.5K+
P: 2,851
That can be done with a while loop:
Expand|Select|Wrap|Line Numbers
  1. >>> while True:
  2. ...     num1 = int(raw_input('Enter a small number: '))
  3. ...     num2 = int(raw_input('Enter a larger number: '))
  4. ...     if not num2 >= num1:
  5. ...         print 'The number must be larger than the first...'
  6. ...     else:
  7. ...         break
  8. ...     
  9. The number must be larger than the first...
  10. >>> 
Oct 1 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.