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 am trying to write little quiz to give different answer when given yes or no reply, however I got stuck with this. I am very very new to Python so please have patience. Can someone give me a sugesstions?
This is my code so far:
name = raw_input("\nWhat is your name\n\n")
print "\nHello, " + name + "."
age = raw_input("\nHow old are you\n\n")
print "\nYou are old, "
question = raw_input("\nCan I ask you one more question?\n\n")
yes = question
no = questions
if question == yes:
print("\nOK, thats good\n\n, ")
car = raw_input("\nWhat car do you drive\n\n")
print "\nThats nice car,what type? "
ok ... i am a little confused (sometimes easily done).
You assign a return value to question:
Quote:
question = raw_input("\nCan I ask you one more question?\n\n")
And we won't go into the lack of checking or even the fact you do not say how to answer (ie what if user just puts a Y).
Then you go on to assign this value to another variable:
Quote:
yes = question
Then you assign a new variable to equal another new variable:
Quote:
no = questions
But I am going to guess this may have been a typo??
From here you do an if that can never be false:
Quote:
if question == yes:
This asks is value stored in question equal to the value stored in yes!!
How can they not be equal when you only assign the value a couple of lines above??
Lastly, we have our typo again:
Quote:
if questions == print("\nwhy not?\n")
What the, if a variable equals a print statement?
I will admit I am newish to Python myself, but you might need to look at all of these.
I am trying to write little quiz to give different answer when given yes or no reply, however I got stuck with this. I am very very new to Python so please have patience. Can someone give me a sugesstions?
Denis
The main reason fro using Python is age. If you are early 50's, like I am, you must have seen "Monty Python's flying circus" on the tube, in the cinema, and all the other media of the era. You probably can cite the Parrot Sketch (This is a norwegian blue!) from your head. If all of this lines up, you use Python as your primary language. As a tribute to Eric Idle and John Cleese. And Michael Palin. In all other cases: stay as far away from Python as possible.
For newbie programmers, nothing beats Modula-2 or Pascal. Ultra structured languages that will teach you how to write code that works. That is error free. That is re-usable. That is extensible. That is OO without ever using the OO letters.
MODULE try;
IMPORT InOut;
TYPE string : ARRAY [0..31] OF CHAR;
VAR name, question : string;
age : CARDINAL;
BEGIN
InOut.WriteString ("Enter your name : ");
InOut.ReadString (name);
InOut.WriteLn;
InOut.WriteLn;
InOut.WriteString ("Hello, ");
InOut.WriteString (name);
InOut.Write (".");
InOut.WriteLn;
InOut.WriteString ("How old are you : ")
InOut.ReadCard (age);
InOut.WriteLn;
InOut.WriteString ("You are old, ");
END try.
Perhaps you may say: but that's a lot of typing to do, I don't want that, I want unreadable short code. OK, that may be your wish. But the penalty of hard to read (and write) code is that it is also hard to get the bugs out.
If Mocka (the Modula-2 compiler) does not coke on my code, it runs. Period. It may not do what I was looking for, but the program does at least part of what was my intention. If there is any kind of error, compilation aborts and tells you so.
If in Modula-2 you do:
Code:
IF car = print (thingies) THEN END
the compiler will tell you this is WRONG. 'car' and 'print' have different TYPE's so they cannot be compared in the first place. Let alone assigned. The c9mpiler may be 'picky', but it is picky to HELP you.
I would agree fruttenboel (even if he is not the messiah) that Python is not an easy language to start with, but that shouldn't detract from you learning it.
Sergei to has pointed out a valid point that you should be able to verbally say what you are doing, ie as if explaining to another person what will happen.
This is termed pseudo code which means irrelevant of language to be used, all programmers should read this and then write the necessary version in their language.
For example:
1. Ask user to enter their name
2. Display greeting with users name
3. Ask user to enter their age
4. Take the mickey out of them irrelevant how old they are (which is how this part of your code reads - print "\nYou are old, ")
So as Sergei has outlined, please complete this process for the remaining steps within your script.
I would agree fruttenboel (even if he is not the messiah)
He's just a very naughty boy.
However, picking a language that will help you is a solid suggestion. There are lots of tutorials and books fanning the hype behind Python (I have several on my shelf) but it's not really the best language to get started and to understand programming.
If you are doing a course where someone is assisting you great run with the language they have chosen otherwise try and find a language that supports your learning and Pascal (plus it's descendants) do that better than most.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.