LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2012, 06:13 PM   #1
toli
LQ Newbie
 
Registered: Jan 2012
Posts: 11

Rep: Reputation: Disabled
Learning Python beginner Help


Hi guys,

I just started to learn Python and I`m a beginner in programming at all. Now I`m trying to get input from user, but when there is no value I get an error and could not continue with my loop.

Code:
while guest_age == '' or guest_age == 0:
	guest_age=input("Your age: ")
This returns an error if the user just press enter.

Code:
Traceback (most recent call last):
  File "<pyshell#150>", line 2, in <module>
    guest_age=input("Your age: ")
  File "<string>", line 0
    
   ^
SyntaxError: unexpected EOF while parsing
Thank you
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-30-2012, 03:32 AM   #2
toli
LQ Newbie
 
Registered: Jan 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
I was thinking tha will be easy to discover for you guys, but it seams the issue is hard to discover..
 
Old 01-30-2012, 03:39 AM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

I'm no developer or Python 'guru', just started learning (and still learning) Python a couple of months ago but it might be a good idea to post your entire script and not just a small part. Furthermore, you might have to exercise some patience, we're all volunteers here putting in our free time. Bumping your thread like this might have an opposite effect.

Kind regards,

Eric
 
Old 01-30-2012, 03:47 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Try
Code:
guest_age=raw_input("Your age: ")
Python help says
Quote:
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function input>
Namespace: Python builtin
Docstring:
input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).
I think the problem is in eval('') call (with empty string as argument).
 
Old 01-30-2012, 01:40 PM   #5
toli
LQ Newbie
 
Registered: Jan 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thank you for your replay guys,

I`m practicing with all the knowedge I have till the moment the code it self works fine, but if the user hits enter without value there the problem is...

Here is my code that works, but if all the values are present:


Code:
guest_name=""
guest_age=0

while guest_name == "":
    guest_name = raw_input("What is your name: ")
while guest_age == 0:
    guest_age = input("How old are you: ")
    
compare_name = raw_input("The name of the person you want to compare to: ")
compare_age = input("How old is " + compare_name + "? : ")

if guest_age < compare_age:
    compbig = compare_age - guest_age
    compbigstr = str(compbig)
    print compare_name + " is " + compbigstr + "y. older than " + guest_name
elif guest_age > compare_age:
    guestbig = guest_age - compare_age
    guestbigstr = str(guestbig)
    print guest_name + " is " + guestbigstr + "y. older " + compare_name
else:
   print guest_name + " has the same age as " + compare_name
raw_input("Press <Enter> to exit:")
 
Old 01-30-2012, 01:58 PM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi,

replace all occurences of input("...") by raw_input("...").
raw_input() just reads a string from standard input. That is what you want.
input() reads a string from standard input and evaluates it as a python expression, which is not what you want.
 
2 members found this post helpful.
Old 01-30-2012, 02:09 PM   #7
toli
LQ Newbie
 
Registered: Jan 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
Hi,

replace all occurences of input("...") by raw_input("...").
raw_input() just reads a string from standard input. That is what you want.
input() reads a string from standard input and evaluates it as a python expression, which is not what you want.
Thank you,

It works, but I need this value as integer, so after the raw_input I do
Code:
guest_age = int(guest_age)
is this normal in programming ?

Thanks
 
Old 01-30-2012, 11:19 PM   #8
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Quote:
Originally Posted by toli View Post
It works, but I need this value as integer, so after the raw_input I do
Code:
guest_age = int(guest_age)
is this normal in programming ?
You should check if guest_age is a valid string before trying to convert it to integer. Look here and here for examples of how to do that.
 
Old 01-31-2012, 08:40 AM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by firstfire View Post
You should check if guest_age is a valid string before trying to convert it to integer. Look here and here for examples of how to do that.
True, and both links correctly recommend the Pythonic idiom of wrapping the conversion in try... except.

However, since you obviously want a non-negative integer, I would just test it with:

Code:
if guest_age.is_digit():

Last edited by dugan; 01-31-2012 at 08:43 AM.
 
Old 01-31-2012, 08:49 AM   #10
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by dugan View Post
However, since you obviously want a non-negative integer, I would just test it with:

Code:
if guest_age.is_digit():
Agree, but it should be
Code:
if guest_age.isdigit():
(without underscore)
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner Linux-only tools and info on learning C programming? linus72 Programming 1 02-23-2009 11:08 AM
Beginner Python Question Chronothread Programming 5 08-05-2008 11:12 PM
python help for beginner thtr2k Programming 1 05-03-2005 03:29 PM
Python - Beginner to Solid Programmer SirLancelotX Programming 3 07-08-2003 02:31 AM
Try Python, O'reilly Learning Python haknot Programming 5 02-15-2002 08:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration