ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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:")
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.