#Size of floating-point values
import sys
x=0.0
print "The size of floating-point variables on this computer is "
print sys.getsizeof(x) #depends on the compiler
#Illustrates the sequence point.
num=5
sq,cube=num*num,num*num*num
#Result
print "The square of ",num,"is",sq
print "and the cube is ",cube
#Uses a bitwise & to determine whether a number is odd or even.
input1=input("What number do you want me to test?")
if input1&1:
print "The number ",input1,"is odd"
else:
print "The number ",input1,"is even"
ans=raw_input("Do you want to continue (Y/N)")
while ans!='Y' and ans!='N':
print "\nYou must type a Y or an N\n"
ans=raw_input( "Do you want to continue(Y/N)?")
#Number of letters in the user's name
name=raw_input("What is your first name?")
count=len(name)
print "Your name has ",count,"characters"
print "*** Inventory computation ***\n"
while True:
part_no=input("What is the next part number(-999 to end)?\n")
if part_no!=-999:
quantity=input("How many were bought?\n")
cost=input("What is the unit price of this item?\n")
ext_cost=cost*quantity
print "\n",quantity,"of #",part_no,"will cost","%.2f" %ext_cost,"\n"
else:
break
print "End of Inventory computation"
#Demonstrates the Break statement
while True:
print "C++ is fun!\n"
break
user_ans=raw_input( "Do you want to see the message again(Y/N)?")
print "That's all for now"
ctr=0
while ctr<10:
print "Computers are fun!\n"
ctr+=1
stored_pass=11862
num_tries=0
while num_tries<3:
user_pass=input("\nWhat is the password? (you get 3 tries...)?")
num_tries+=1
if user_pass==stored_pass:
print "You entered the correct password.\n"
print "The cash safe is behind the picture of the ship."
exit()
else:
print "You entered the wrong password.\n"
if num_tries==3:
print "Sorry, you get no more chances"
else:
print "you get ",3-num_tries,"more tries...\n"
exit(0)
#Adds grades and determines whether you earned an A.
total_grade=0.0
while 1:
grade=input("What is your grade?(-1 to end)")
if grade>=0.0:
total_grade+=grade
if grade==-1:
break
#Result
print "\n\nYou made a total of ","%.1f" %total_grade,"points\n"
if total_grade>=450.00:
print "** You made an A !!"
total_grade=0.0
grade_avg=0.0
grade_ctr=0
while 1:
grade=input("What is your grade?(-1 to end)")
if grade>=0.0:
total_grade+=grade
grade_ctr+=1
if grade==-1:
break
grade_avg=total_grade/grade_ctr
#Result
print "\n\nYou made a total of ",'%.1f' %total_grade,"points\n"
print "Your average was ",'%.1f' %grade_avg,"\n"
if total_grade>=450.00:
print "** You made an A !!"
#for loop example
for ctr in range(1,11):
print ctr,"\n"
#Demonstrates totaling using a for loop.
total=0
for ctr in range(100,201):
total+=ctr
#Result
print "The total is ",total
print "Even numbers below 21"
#Result
for num in range(2,21,2):
print num," ",
print "\n\nOdd numbers below 20"
#Result
for num in range(1,21,2):
print num," ",
for ctr in range(10,0,-1):
print ctr
print "*** Blast off! ***\n"
total=0.0
print "\n*** Grade Calculation ***\n"
num=input("How many students are there?\n")
for loopvar in range(0,num,1):
grade=input("What is the next student's grade?\n")
total=total+grade
avg=total/num
#Result
print "\n the average of this class is",'%.1f' %avg
num=5
print "\nCounting by 5s:\n"
for num in range(5,101,5):
print "\n",num
for times in range(1,4,1):
for num in range(1,6,1):
print num,
print "\n"
#for loop
for outer in range(6,-1,-1):
for inner in range(1,outer,1):
print inner,
print "\n"
#factorial
num=input("What factorial do you want to see?")
total=1
for fact in range(1,num+1,1):
total=total*fact
print "The factorial for ",num,"is",total
for cd in range(10,0,-1):
for delay in range(1,10,1): #for delay in range(1,30001,1):
print " "
print cd,"\n"
print "*** Blast off! ***\n"
age=input("What is your age?\n") #Get age
while age<=0:
print "*** Your age cannot be that small ! ***"
for outer in range(1,3,1): #outer loop
for inner in range(1,50,1): #inner loop
print ""
print "\r\n\n"
age=input("What is your age?\n")
print "Thanks, I did not think you would actually tell me your age!"
print "Here are the numbers from 1 to 20\n"
for num in range(1,21,1):
print num,"\n"
break #break statement
print "That's all, folks!"
#A for loop running at the user’s request.
print "Here are the numbers from 1 to 20\n"
for num in range(1,21,1):
print num
ans=raw_input("Do you want to see another (Y/N)?")
if ans=='N' or ans=='n':
break
print "That's all, folks!\n"
total=0.0
count=0
print "\n*** Grade Calculation ***\n"
num=input("How many students are there?")
for loopvar in range(1,num+1,1):
grade=input("What is the next student's grade? (-99 to quit)")
if grade<0.0:
break
count+=1
total+=grade
avg=total/count
#Result
print "The average of this class is ",'%.1f' %avg
#Demonstrates the use of the continue statement.
for ctr in range(1,11,1):
print ctr,
continue
print "C++ programming"
#Average salaries over $10,000
avg=0.0
total=0.0
month=1.0
count=0
while month>0.0:
month=input("What is the next monthly salary (-1) to quit")
year=month*12.00
if year <= 10000.00: #Do not add low salaries
continue
if month<0.0:
break
count+=1
total+=year #Add yearly salary to total.
avg=total/float(count)
#Result
print "\nThe average of high salaries is $",'%.2f' %avg
def beep():
import os
os.system('\a')
num=input("Please enter a number:")
#Use multiple if statements to beep.
if num==1:
beep()
else:
if num==2:
beep()
beep()
else:
if num==3:
beep()
beep()
beep()
else:
if num==4:
beep()
beep()
beep()
beep()
else:
if num==5:
beep()
beep()
beep()
beep()
beep()
#Prints daily, weekly, and monthly sales totals.
daily=2343.34
weekly=13432.65
monthly=43468.97
ans=raw_input("Is this the end of the month? (Y/N) :")
if ans=='Y' or ans=='y':
day=6
else:
day=input("What day number , 1 through 5( for mon-fri) :")
if day==6:
print "The monthly total is ",'%.2f' %monthly
else:
if day==5:
print "The weekly total is ",'%.2f' %weekly
else:
print "The daily total is ",'%.2f' %daily
choice="R"
while choice!='S' and choice!='A' and choice!='E' and choice!='P':
print "\n choose your department :"
print "S - Sales"
print "A - Accounting"
print "E - Engineering"
print "P - Payroll"
choice=raw_input( "What is your choice? (upper case)")
if choice=='E':
print "Your meeting is at 2:30"
else:
if choice=='S':
print "Your meeting is at 8:30"
else:
if choice=='A':
print "Your meeting is at 10:00"
else:
if choice=='P':
print "your meeting has been cancelled"