Welcome Guest ( Log In | Register )

IPB

 
RépondreNouveau sujet
» Help with Python
Cory Duchesne
post Mar 11 2010, 11:40 PM
Post #1


Member
Group Icon

Group: Premium
Posts: 42
Joined: 9-January 09
Member No.: 11,092




Hello.

I'm doing some really basic online tutorials that teach python. So far it's been really basic stuff on operators, and the code that is provided, when you run it, works as it should. However, I've reached a point where I'm learning about breaks and 'the continue statement'.

To learn about breaks, the tut gives the following code to run:

CODE

#!/usr/bin/python
# Filename: break.py

while True:
s = (input('Enter something : '))
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')


The output of this code is supposed to look like this:


CODE

$ python break.py
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something : use Python!
Length of the string is 12
Enter something : quit
Done


If you run this program, you'll notice that when you enter something (e.g., programming is fun), you'll get a red error message.

I discovered that you can get around this error message by wrapping the inputted string with single quotations:

'programming is fun'

The single quotations seem to fix the problem.

But why can't I enter just ordinary strings? Why do I have to use single quotations? And how can I write my code so that I don't require single quotations around my inputted strings?


PS: I use IDLE as my code editor

This post has been edited by Cory Duchesne: Mar 11 2010, 11:41 PM
Go to the top of the page
 
+Quote Post
Nano
post Mar 12 2010, 04:13 PM
Post #2


Command-line Adept
Group Icon

Group: Support Staff Lv1
Posts: 838
Joined: 28-May 07
From: Olympia, WA, USA
Member No.: 4,415




You need to replace input with raw_input in order for it to return a string.

Here's a good example on how they differ - http://www.wellho.net/resources/ex.php4?item=y102/yraw

This post has been edited by Nano: Mar 12 2010, 04:15 PM


--------------------

OpenOffice | VLC | Ubuntu | Paint.NET | Chrome
If my post was helpful, a thank you is much appreciated!
Are you new? Check out the Rules and FAQs!
Go to the top of the page
 
+Quote Post
Cory Duchesne
post Mar 12 2010, 05:14 PM
Post #3


Member
Group Icon

Group: Premium
Posts: 42
Joined: 9-January 09
Member No.: 11,092




QUOTE (Nano @ Mar 12 2010, 04:13 PM) *
You need to replace input with raw_input in order for it to return a string.

Here's a good example on how they differ - http://www.wellho.net/resources/ex.php4?item=y102/yraw


Ah, perfect, thanks Nano.
Go to the top of the page
 
+Quote Post

Reponse rapideRépondreNouveau sujet
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 




Lo-Fi Version
Time is now: 29th July 2010 - 03:31 PM
Skin by IPB FR - IPB Europe