1.3 Hello World
#Tasks:
# Use the print statement to display some expressions.
print "Hello World"
# Find a way to have print statements on different lines of my program
# display on the same line of my console.
print "Hello",
print "World"
1.4 Echo User Input
#Tasks:
# Write a program that echoes user input several times
print raw_input("First word? ")
print raw_input("Second word? ")
# What type does raw_input() return?
print type(raw_input())
1.6 Simple Calculator
#Task:
# Create a simple calculator program
# It should ask for two numbers and print their
# sum, difference, product, and quotient
# This program assumes the numbers are integers, and will truncate any
# numbers that aren't!
a = int(raw_input("First number: "))
b = int(raw_input("Second number: "))
print "Sum:",a+b
print "Difference (first - second):",a-b
print "Product:",a*b
print "Quotient (first/second ignoring remainder):",a/b
print "Quotient (first/second with remainder):", 1.0*a/b
# Modify the calculator to also report the length of the hypotenuse of
# a right triangle with the given two integers as legs.
import math
print "Hypotenuse:",math.sqrt(a**2 + b**2)
1.11 Guessing Game
#Task:
# Construct a program which plays a guessing game, narrowing down the
# possibilities with questions until it is able to guess the correct
# person.
# (Your solution will have different details.)
#I will be using a selection of pokemon for my example:
#Pikachu, Zubat, Pidgey, Charmander, Bulbasaur, Squirtle, Geodude, Onyx,
# and Dragonite
print "Please think of one of the following pokemon:"
print "Pikachu, Zubat, Pidgey, Charmander, Bulbasaur, Squirtle,",
print "Geodude, Onyx, or Dragonite"
starting = raw_input("Are you thinking of a starting pokemon? (y/n) ")
if starting == "y": #Pikachu, Charmander, Bulbasaur, or Squirtle
p = "Is your pokemon super-effective against water type pokemon?"
effective = raw_input(p + " (y/n) ")
if effective == "y":
p = "Are you thinking of the most famous pokemon of all? (y/n) "
fame = raw_input(p)
if fame == "y":
print "You are thinking of Pikachu!"
else:
print "You are thinking of Bulbasaur!"
else:
p = "Does your pokemon gain flying type when it evolves? (y/n) "
flying = raw_input(p)
if flying == "y":
print "You are thinking of Charmander!"
else:
print "You are thinking of Squirtle!"
else: #Zubat, Pidgey, Geodude, Onyx, Dragonite
rock = raw_input("Is your pokemon rock type? (y/n) ")
if rock == "y":
hands = raw_input("Are you thinking of a pokemon with hands? (y/n) ")
if hands == "y":
print "You are thinking of Geodude!"
else:
print "You are thinking of Onyx!"
else: #Zubat, Pidgey, Dragonite
ptype = raw_input("Aside from flying, what type is your pokemon? ")
if ptype == "poison" or ptype == "Poison":
print "You are thinking of Zubat!"
elif ptype == "normal" or ptype == "Normal":
print "You are thinking of Pidgey!"
elif ptype == "dragon" or ptype == "Dragon":
print "You are thinking of Dragonite!"
else:
print "Sorry. I didn't understand that."