My Python 3 mini-programs! (including a sandwich maker)

Robo

Sup
Joined
Apr 17, 2013
Messages
663
Reaction score
637
Points
243
Hi, I was talked into learning code by the fabulous Sheepface (Fiestaguy ). The language I have been learning is Python 3. I'm still a beginner at it, and don't know some things, but I have managed to make my own mini-program (not sure what its supposed to be called) which will make you a sandwich! Check it out here: http://repl.it/ebh/2
Again, I'm new to Python, and only just started a few days ago, but I wanted to share this with you to see what you think.

What did your sandwich contain?
Peanut butter and jelly
Potato and lollipops
Mayo, ham, and cheese
 

Ltin

Member
Mafia Host
Joined
Apr 7, 2013
Messages
951
Reaction score
1,481
Points
93
Ltin 's sandwich will have:
bread
Bread
Peanut butter
Jelly
bread
I am making your sandwich now, Ltin
...
...
Your sandwich is ready, Ltin
*gives sandwich*
 

Fiestaguy

The man with 8 fingers.
Joined
Sep 11, 2013
Messages
2,197
Reaction score
1,643
Points
138
Website
thebes.openshells.net
Here's a few criticisms (or tips) on your code:
Code:
print(x,'\'s sandwich will have:')
You can avoid the Escape-sequence entirely by enclosing this string in double parentheses'

Apart from that, try:
Code:
print(x+"'s sandwich will have:")
Replacing the comma after 'x' with a + will remove the space in the output, making your code more readable.
Apart from that: I don't know python to blarggghhh.
 
  • Thank You
Reactions: Robo

myusername22

A username.
Donor
Joined
Dec 17, 2012
Messages
858
Reaction score
937
Points
93
Website
escaperestart.com
What is your name? James
I will make you a sandwich James
What would you like on your sandwich? everything
and
peanutbutter
and
nothing
James 's sandwich will have:
bread
everything
peanutbutter
nothing
bread
I am making your sandwich now, James
...
...
Your sandwich is ready, James
*gives sandwich*
What is your name?
I will make you a sandwich
What would you like on your sandwich? Can i have my sandwhich without bread?
and
i'm serious please no bread

and

I said no bread!!!
's sandwich will have:
bread
Can i have my sandwhich without bread?
i'm serious please no bread
I said no bread!!!
I am making your sandwich now,
...
...
Your sandwich is ready,
*gives sandwich*
 
Last edited:

san00b

No.1 Admin & Founder
Joined
Aug 2, 2011
Messages
154
Reaction score
560
Points
243
Some ideas about where you could go from here with your sandwich maker:
  • Turn Sandwich into a class, including the standard "__init__" method, along with a "make" method.
  • Instead of prompting the user for ingredients one by one, ask them to enter a comma separated list of ingredients. Use that to create a Python list.
  • Try some input validation - only offer a few things, and make sure that the user only picks from those specific things.
Hope it helps!
 

Robo

Sup
Joined
Apr 17, 2013
Messages
663
Reaction score
637
Points
243
Hi! I talked with SirComputer a bit, and he helped me figure out how to make code answer questions and ask questions. So I made a quiz! It has simple, and quite interesting questions, but its neat. Check it out here http://repl.it/fOY/4
I'm soon going to see if I can make a bot for IRC. I already have some ideas for what it'll do.
 

Malcovent

Geezuslike
Donor
Contributor
Joined
Aug 5, 2011
Messages
1,148
Reaction score
3,081
Points
263
Hi! I talked with SirComputer a bit, and he helped me figure out how to make code answer questions and ask questions. So I made a quiz! It has simple, and quite interesting questions, but its neat. Check it out here http://repl.it/fOY/4
I'm soon going to see if I can make a bot for IRC. I already have some ideas for what it'll do.
Just my personal advice, focus on quality - some areas you could expand your code (Pythons not really my area so apologies for any errors)

- General advice that most guides will tell you. Focus on dabbling in as many general programming features & structures as possible (Conditional statements, Arrays/Hashmaps, etc.) - they are the bread, butter and cheese of most programs, and you will struggle to get far (if anywhere) without learning them.-

- You didn't need to create three seperate 'varQuiz' variables. You could've re-used one. Good naming plays an unexpectedly large role in programming, ideally your identifier should only reference what it's doing, so in this case it should've simply be named "answer" or "userResponse", "varQuiz" doesn't describe the variable at all really. Golden standard here should be that you could give the variable name to a non-programmer without the context and they can identify its usage.

- Rather than checking for an exact response, check for a.) A partial response b.) Ignore case sensitivity, for example for question 2 (Grass blue, etc.) rather than checking for a case sensitive "False", just change the users answer to lowercase and check for lowercase 'f' in the answer. Already there's a slight issue with stating to the user "True or false" and only accepting "False" with an uppercase F - despite prompting the user for 'false'.

- Really nitpicky: Whilst i personally try to make most/all of my magic numbers/Strings a constant declared at the start of a method/class, in this case you might want to at least create a constant String for 'thats correct!' and 'thats wrong!' and reference them in the user prompt, this is a very very basic case of the 'DRY' principle (Don't repeat yourself - if you so much as touch copy + paste when programming, ask whether you really need to copy paste or if you can move it to its own independent variable/method and reference that) the reasons behind this are thus
  • - If you needed to change "Thats wrong" to "Thats incorrect", at the moment you would need to alter two separate sentences. If this had fifty questions, you might need to alter FIFTY sentences - or, if you kept the response in a single variable and reference that, you only need to change a single sentence. It greatly reduces the risk of forgetting to alter one of many many sentences (which can introduce errors). Whilst it may seem like it'd take longer to move it to its own constant/reference that constant - it'll save you a lot of time in future and keep your code neater. A stitch in time saves nine.

- To expand your code out, though you may not be able to share this via repl.it and, generally something you should learn as part of the basics, you could keep a text file of questions/answers in whatever format (Maybe question colon answer) - read it line by line, and break it down into a list of questions/answers, you'd then have the ability to ask the user random questions and would be able to avoid hardcoding questions/answers/magic strings.

- For full 'hardmode' at that level of learning you could have each line in a text file containing the following. [questions]:[full answer]:[keywords] - this allows you to implement my first point, searching peoples response for lowercased key words rather than checking the entire string.

- If you want to hook this into an Ircbot at some stage, the functionality to load questions/have users answer them quite easily slips into the role of a quizbot (assuming you want to custom design it). Hell why stop there, once you get the full hang of things you could even make a Countdown bot (or any other question/answer based gameshow) for irc.


Edit:Heres a working code example with some of the suggested cleanups, feel free to ask any questions or hit me up on steam.
 
Last edited:
  • Like
Reactions: Hunter and Naoh

Robo

Sup
Joined
Apr 17, 2013
Messages
663
Reaction score
637
Points
243
Just my personal advice, focus on quality - some areas you could expand your code (Pythons not really my area so apologies for any errors)

- General advice that most guides will tell you. Focus on dabbling in as many general programming features & structures as possible (Conditional statements, Arrays/Hashmaps, etc.) - they are the bread, butter and cheese of most programs, and you will struggle to get far (if anywhere) without learning them.-

- You didn't need to create three seperate 'varQuiz' variables. You could've re-used one. Good naming plays an unexpectedly large role in programming, ideally your identifier should only reference what it's doing, so in this case it should've simply be named "answer" or "userResponse", "varQuiz" doesn't describe the variable at all really. Golden standard here should be that you could give the variable name to a non-programmer without the context and they can identify its usage.

- Rather than checking for an exact response, check for a.) A partial response b.) Ignore case sensitivity, for example for question 2 (Grass blue, etc.) rather than checking for a case sensitive "False", just change the users answer to lowercase and check for lowercase 'f' in the answer. Already there's a slight issue with stating to the user "True or false" and only accepting "False" with an uppercase F - despite prompting the user for 'false'.

- Really nitpicky: Whilst i personally try to make most/all of my magic numbers/Strings a constant declared at the start of a method/class, in this case you might want to at least create a constant String for 'thats correct!' and 'thats wrong!' and reference them in the user prompt, this is a very very basic case of the 'DRY' principle (Don't repeat yourself - if you so much as touch copy + paste when programming, ask whether you really need to copy paste or if you can move it to its own independent variable/method and reference that) the reasons behind this are thus
  • - If you needed to change "Thats wrong" to "Thats incorrect", at the moment you would need to alter two separate sentences. If this had fifty questions, you might need to alter FIFTY sentences - or, if you kept the response in a single variable and reference that, you only need to change a single sentence. It greatly reduces the risk of forgetting to alter one of many many sentences (which can introduce errors). Whilst it may seem like it'd take longer to move it to its own constant/reference that constant - it'll save you a lot of time in future and keep your code neater. A stitch in time saves nine.

- To expand your code out, though you may not be able to share this via repl.it and, generally something you should learn as part of the basics, you could keep a text file of questions/answers in whatever format (Maybe question colon answer) - read it line by line, and break it down into a list of questions/answers, you'd then have the ability to ask the user random questions and would be able to avoid hardcoding questions/answers/magic strings.

- For full 'hardmode' at that level of learning you could have each line in a text file containing the following. [questions]:[full answer]:[keywords] - this allows you to implement my first point, searching peoples response for lowercased key words rather than checking the entire string.

- If you want to hook this into an Ircbot at some stage, the functionality to load questions/have users answer them quite easily slips into the role of a quizbot (assuming you want to custom design it). Hell why stop there, once you get the full hang of things you could even make a Countdown bot (or any other question/answer based gameshow) for irc.


Edit:Heres a working code example with some of the suggested cleanups, feel free to ask any questions or hit me up on steam.
I mean... I only just started.. o.o
So I guess some day I'll understand all of what you said here. Other than that, thanks for the info!
 

Malcovent

Geezuslike
Donor
Contributor
Joined
Aug 5, 2011
Messages
1,148
Reaction score
3,081
Points
263
I mean... I only just started.. o.o
So I guess some day I'll understand all of what you said here. Other than that, thanks for the info!
Understandable :) Like I said, my points are ordered (at least in my opinion) from the most to least important - so the most important thing is toy around with conditional statements/loops/Data structures like maps/arrays. Everything else I said is mostly about cleaning up your code/can be ignored until you're more comfortable with programming.
 
  • Like
Reactions: myusername22

san00b

No.1 Admin & Founder
Joined
Aug 2, 2011
Messages
154
Reaction score
560
Points
243
Some Python-specific direction for what Malcovent is talking about:
  • Lists, dictionaries, tuples, strings, integers, and floats are the bread and butter of Python. Learn these well, what they are used for, and how to interact with them. There are certainty more than just that, but those you'll want to know inside and out.
  • In Python, the naming convention for variables is different depending on their scope. For 99% of cases though, and for your Quiz script, you'll want to use the "name_with_underscores" style. For style questions, always refer to the PEP8, and https://google-styleguide.googlecode.com/svn/trunk/pyguide.html
  • For pattern matching, look into the Python 're' library. It's the regular expression library that will allow you to do all sorts of neat things. However, regular expressions get complicated very quickly, and I wouldn't recommend spending too much time here until you're more comfortable with the language.
For where you're at, I highly recommend 'Learn Python the Hard Way'. It's completely free to read online: http://learnpythonthehardway.org/book/ . Out of all the various books I have at my desk in the office, this is the one people request the most, and I get the best feedback about. Start at the Introduction, as the Preface is just him asking you to buy the book if you enjoy it.

Hope it helps!