473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
3 2345
bvdet
2,851 Expert Mod 2GB
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
Fuugie
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
2,851 Expert Mod 2GB
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

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

Similar topics

10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
4
by: Radioactive Man | last post by:
anyone know of a function like "raw_input", which collects a string from the user entry, but one where I can set a time limit, as follows: time_limit = 10 # seconds user_answer =...
2
by: roopeman | last post by:
i need your help, my code as below : //----------------------------------------------------------------------- //Wrote by Michael April 30 2005...
4
by: SeNTry | last post by:
Hi Everyone, My first post here as I just begin to learn programming in general and python in particular. I have all the noobie confused questions, but as I work thru the tutorials I'm sure...
21
by: planetthoughtful | last post by:
Hi All, As always, my posts come with a 'Warning: Newbie lies ahead!' disclaimer... I'm wondering if it's possible, using raw_input(), to provide a 'default' value with the prompt? I would...
7
by: c676228 | last post by:
Hi all, I have the code: If Session("Plan") = "Plan I" Then... I was just aware of that the Session("Plan") is an object, with String value "Plan I", but I don't know how to compare them and it...
5
by: Alex Pavluck | last post by:
Hello. I get the following error with the following code. Is there something wrong with my Python installation? code: import types something = input("Enter something and I will tell you the...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
6
by: 7stud | last post by:
s = 'A\xcc\x88' #capital A with umlaut print s #displays capital A with umlaut s = raw_input('Enter: ') #A\xcc\x88 print s #displays A\xcc\x88 print...
5
by: jamitwidme | last post by:
Is there any way to type into a Tkinter frame window? I want to use raw_input() within a Tkinter frame.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.