
- Read more...
-
- 0 comments
- 357 views
I started this blog having written about 10 small programs in Python and getting most of them to work - 90 pages into my first python book.
My goal is to code my own indie video games.
I'm studying python and unity.
I'll talk about things I did wrong and what I learned. Hopefully I can chart my progression on this blog.
For more of my stuff check out my website: http://imagineermike.com
tip15 = 0
tip20 = 0
price = input ("how much did your meal cost?")
tip15 = int (price) * .15
tip20 = int (price) * .2
print ("A 20% tip would be ",tip20," and a 15% tip would be",tip15)
input ()
I wrote a tipper program for an exercise.
It's a simple program and it took 20mins to write including several bug fixes,
where I had to go back to looking through the textbook and a few glances at my first program,
before I got it running properly. I made the usual noob mistakes - since I've tackled many different languages, I had to figure out if I needed to declare variables.
I also got the variable on the wrong side of the equals sign, then I forgot to include commas in my print statement for the variables. I also forgot to state that the variable was an integer. Finally, I forgot to use brackets and then incorrectly included the calculation inside the brackets.
Eventually, I figured all of this out and came up with the above seemingly simple program.
And it works!
I'm still not using comments, but I'll fix that when I start writing longer programs.
carPrice = input ("what is the base price of the car?")
tax = int (carPrice) * .125
insurance = 250
totalcarPrice = int (carPrice) + int (insurance) + int (tax)
print ("total cost of your car including: insurance $",insurance,",")
print ("and tax: $",tax," comes to $",totalcarPrice)
input ()
Then the program provides the user with both the individual costs, and the total all-inclusive price of the car.
EDIT:
After some research not in-book, it turns out that you can avoid the white spaces in between statements by using the function sep = "", which should be treated as a variable - so not inside the quotation marks of the print function, rather, naked inside the brackets.
So the final program now looks like this:
carPrice = input ("what is the base price of the car?")
tax = int (carPrice) * .125
insurance = 250
totalcarPrice = int (carPrice) + int (insurance) + int (tax)
print ("total cost of your car including: insurance $",insurance,",", sep = "")
print ("and tax: $",tax," comes to $",totalcarPrice, sep = "")
So it took one day to write and bug fix.
Then another day to go through guesswork and figuring it out - to get the program to work.
I haven't got up to while loops in the book, so it took a while - a few errors before I got it working.
And I certainly haven't got as far as def methods, so using them was difficult and problematic. But I wanted my program to be complete with an exit option.
import random
def end_game():
end_message = ("game over")
print (end_message)
def game():
againPlay = "y"
while againPlay == "y":
nmCookie = random.randrange(5)
begin = input ("cookie time, open your fortune cookie")
if nmCookie < 1:
print ("you are going to die someday")
againPlay = input ("Still hungry")
elif nmCookie == 1:
print ("you just ate a cookie")
againPlay = input ("Still hungry")
elif nmCookie == 2:
print ("you are going to eat another cookie")
againPlay = input ("Still hungry")
elif nmCookie == 3:
print ("you like cookies")
againPlay = input ("Still hungry")
elif nmCookie == 4:
print ("you will have a gargantuan legacy")
againPlay = input ("Still hungry")
else: end_game()
game ()
end_game ()
By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.
Participate in the game development conversation and more when you create an account on GameDev.net!
Sign me up!