SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 13th, 2004, 12:14 AM
member009 member009 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 2 member009 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
comparing user input against a string (newbie)

Hi, i'm trying to break out of a loop whenever user type in "q".
However if ui=="q": never return true when i type in "q"!?!?
what is the reason?


while:
print "Pls input the nominated text: "
ui = str(sys.stdin.readline())
if ui=="q":
break

Reply With Quote
  #2  
Old March 13th, 2004, 02:29 AM
dmills dmills is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 5 dmills User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb Re: comparing user input against a string (newbie)

I believe the problem is that sys.stdin.readline() will read in the whole line, including the carriage return ("\n") at the end. According to the Python docs:

Quote:
readline( [size])
Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line).


If you wish to remove the newline character try rstrip(). Here's your code modified to (hopefully) work:

Code:
while 1:  
     print "Pls input the nominated text: "  
     ui = sys.stdin.readline()  
     ui = ui.rstrip("\n")
     if ui=="q":  
          break

Reply With Quote
  #3  
Old March 13th, 2004, 02:54 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
An easier way is probably to use the function raw_input(). It will strip the newline from the input.

Reply With Quote
  #4  
Old March 13th, 2004, 09:03 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
A few similar examples of how to do this the easy way.

Code:
#!/usr/bin/env python

user = ''

while user != 'quit':
    user = raw_input('Pls input the nominated text: ')


or

Code:
#!/usr/bin/env python

user = raw_input('Pls input the nominated text: ')

while user != 'quit':
    user = raw_input('Pls input the nominated text: ')


Of course this exits if the user enters 'quit' but not hard to change to 'q'.

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #5  
Old March 13th, 2004, 02:24 PM
MasterChief MasterChief is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Virginia
Posts: 491 MasterChief User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 47 m 47 sec
Reputation Power: 6
Send a message via AIM to MasterChief Send a message via MSN to MasterChief
You should transform the text to be all upper-case letters or all lower-case letters. Then make the quit command 'q' and 'quit'.

So if a user puts in QuIt, it will be transformed into 'quit'.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > comparing user input against a string (newbie)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway