LinuxQuestions.org
Visit Jeremy's Blog.
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 05-20-2010, 10:31 AM   #1
smithy2010
Member
 
Registered: May 2010
Location: UK
Distribution: OpenSuse 64 bit
Posts: 45

Rep: Reputation: 15
Smile yes or no Python


Hi all,

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? "


if questions == print("\nwhy not?\n")

Thank you in advance


Denis
 
Old 05-20-2010, 11:20 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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.
 
Old 05-20-2010, 12:34 PM   #3
fruttenboel
Member
 
Registered: Jul 2008
Location: Tilburg NL
Distribution: Slackware 14.2 ciurrent, kernel 3.18.11
Posts: 270

Rep: Reputation: 48
Thumbs up

Quote:
Originally Posted by smithy2010 View Post
Hi all,

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.

See http://fruttenboel.verhoeven272.nl/mocka/index.html and http://fruttenboel.verhoeven272.nl/modula-2/index.html and perhaps http://fruttenboel.verhoeven272.nl/Oberon/index.html may ring your bell.

Your source in Modula-2:

Code:
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.
 
Old 05-20-2010, 03:27 PM   #4
smithy2010
Member
 
Registered: May 2010
Location: UK
Distribution: OpenSuse 64 bit
Posts: 45

Original Poster
Rep: Reputation: 15
Hi grail,

so how would you write the code then?

can you write example?

Cheers

Denis
 
Old 05-20-2010, 04:04 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smithy2010 View Post
Hi grail,

so how would you write the code then?

can you write example?

Cheers

Denis
Just curious - what did you mean by this code:

Code:
yes = question
no = questions

if question == yes:
print("\nOK, thats good\n\n, ")
?

I.e. could describe what you meant to do in English ?
 
Old 05-20-2010, 07:30 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Hi Denis

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.
 
Old 05-21-2010, 02:40 AM   #7
fruttenboel
Member
 
Registered: Jul 2008
Location: Tilburg NL
Distribution: Slackware 14.2 ciurrent, kernel 3.18.11
Posts: 270

Rep: Reputation: 48
Quote:
Originally Posted by smithy2010 View Post
Hi grail,

so how would you write the code then?
can you write example?

Denis
I would suggest you buy one of these books: http://fruttenboel.verhoeven272.nl/m...iterature.html

Try to get a copy of "Algorithms + Data Structures = Programs" by NIKLAUS WIRTH (for sale for a few dollars from many source).

http://members.home.nl/jmr272/Oberon/AD.pdf
http://www-old.oberon.ethz.ch/WirthP...InOberonWR.pdf
 
Old 05-30-2010, 09:51 AM   #8
smithy2010
Member
 
Registered: May 2010
Location: UK
Distribution: OpenSuse 64 bit
Posts: 45

Original Poster
Rep: Reputation: 15
Thanks for your suggestions, I shell try again...

;-)
 
Old 05-30-2010, 09:32 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by grail View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How can I uninstall a python package installed with python setup.py install? milomak Linux - Software 1 11-16-2009 09:22 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
LXer: Review: Programming in Python 3: A Complete Introduction to the Python Language LXer Syndicated Linux News 0 01-26-2009 04:50 AM
python update - Unable to load GTK2 Python bindings: No module named gtk itpedersen Linux - Software 2 10-03-2008 03:44 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM

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

All times are GMT -5. The time now is 12:18 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