Back

Python Chrestomathics

Ahead


 

Part 4 — Interactivity

Communicating — Back and forth is a means — Brings needed knowledge

np pendulum

Section 4.1: Function input()

reply = input( "Enter text: " )

""" Purpose: demonstrate string acquisition (the getting of input)

"""

# Get and echo back first input

reply = input( "Enter text: " )

print( "You replied:", reply )

print()

# Get and echo back second input

reply = input( "Enter more text: " )

print( "You then replied:", reply )

Enter text: Tell me a secret

You replied: Tell me a secret

Enter more text: What is your name

You then replied: What is your name

Variable Value
reply "Tell me a secret"

You replied: Tell me a secret

Variable Value
reply "What is your name"

You then replied: What is your name

Enter text: 88

You replied: 88

Enter more text:

You then replied:

Variable Value
reply "88"

Variable Value
reply ""



Section 4.2: Function len()

# Get input

reply = input( "Enter text: " )

# Process the input

count = len( reply )

# Print the results

print( reply, "has", count, "characters" )

count = len( reply )

Enter text: go your own way

go your own way has 15 characters

Enter text: strengths

strengths has 9 characters

Enter text: scraunched

scraunched has 10 characters



Section 4.3: Processing numeric inputs

# Get inputs

x_reply = input( 'Enter base x (integer): ' )

n_reply = input( 'Enter exponent n (decimal): ' )

print()

# Massage input to numeric

x = int( x_reply ) # cast string get an int

n = float( n_reply ) # cast string get a decimal

# Compute result -- x to the nth power

result = x ** n

# Display result

print( result )

# Get inputs

x_reply = input( 'Enter base x (integer): ' )

n_reply = input( 'Enter exponent n (decimal): ' )

# Massage input to numeric

x = int( x_reply ) # cast string get an int

n = float( n_reply ) # cast string get a decimal

# Compute result -- x to the nth power

result = x ** n

# Display result

print( result )

Enter base (integer): 2

Enter exponent (decimal): 0.5

 

1.4142135623730951

Enter base (integer): 11

Enter exponent (decimal): 3.14

 

1861.971509290233

Enter base (integer): 4

Enter exponent (decimal): 0.333

 

1.5866676863822857

 


Section 4.4: Type str

It is not enough — Integers and decimals — We need a string type

a = 'day'

b = 'dreamer'

c = a + b

print( a, '+', b, 'is', c )

day + dreamer is daydreamer

one_dash = "-"

n = 50

dashes = n * one_dash

print( n, "*", one_dash, "is", dashes )

dashes = one_dash * n

print( one_dash, "*", n, "is", dashes )

50 * - is --------------------------------------------------

- * 50 is --------------------------------------------------


Section 4.5: String method split()

Something is lacking — A new perspective needed — Splitting can help out

w1, w2, w3 = reply.split()

w1, w2, w3 = reply.split()

Variable Value
reply " you me us  "
w1 "you"
w1 "me"
w1 "us"

# Get input

reply = input( 'Enter three words: ' )

# Determine individual words w1, w2, and w3

w1, w2, w3 = reply.split()

# Display results

print()

print( "Your words:" )

print( w1 )

print( w2 )

print( w3 )

Enter three words: peace joy friends

Your words

peace

joy

friends

Enter three words: ready or not

ready

or

not



Section 4.6: Pyramid power — case study

© noun project pyramidsurface pyramid formula

Enter pyramid height and its base perimeter: 24 56

Surface area: 700.0

# Get height and perimeter inputs

reply = input( "Enter pyramid height and its base perimeter: " )

h, p = reply.split()

Variable Value
reply "24 56"
h "24"
p "56"

# Massage inputs into integers

h = int( h )

p = int( p )

h, p = int( h ), int( p )

Variable Value
reply "24 56"
h 24
p 56

# Compute surface area

radical = ( h*h + ( p/8 * p/8 ) ) ** 0.5

surface = p / 2 * radical

Variable Value
reply "24 56"
h 24
p 56
radical 25.0
surface 700.0

# Print result

print( "Surface area:", surface )

Enter pyramid height and its base perimeter: 15 64

Surface area: 544.0

Enter pyramid height and its base perimeter: 75 400

Surface area: 18027.756377319947



Section 4.7: Expression evaluation and operator precedence

ax2 + bx + c

And also know how to evaluate it. The 2 in the equation is an exponent, and x2 is evaluated. The square is then multiplied by a. That value is added to the product bc. That sum is then added to c.

F conversion to C formula

# Get Fahrenheit input temperature f

reply = input( "Enter temperature (integer): " )

f = int( reply)

# Calculate integer Celsius equivelent

c = 5 // 9 * ( f - 32 )

# Display result

print( f, "°F equals", c, "°C" )

Enter temperature (integer): 32

32 °F equals 0 °C

Enter temperature (integer): 212

212 °F equals 0 °C

Enter temperature (integer): -40

-40 °F equals 0 °C

c = 5 // 9 * ( f - 32 )

# Get Fahrenheit input temperature f

reply = input( "Enter temperature (integer): " )

f = int( reply)

# Calculate integer Celsius equivelent

c = 5 * ( f - 32 ) // 9

# Display result

print( f, "°F equals", c, "°C" )

Enter temperature (integer): 32

32 °F equals 0 °C

Enter temperature (integer): 212

212 °F equals 100 °C

Enter temperature (integer): -40

-40 °F equals -40 °C

# Get Fahrenheit input temperature f

reply = input( "Enter temperature (integer): " )

f = int( reply)

# Calculate integer Celsius equivelent

c = 5/9 * ( f - 32 )

c = int( c )

# Display result

print( f, "°F equals", c, "°C" )

Enter temperature (integer): 32

32 °F equals 0 °C

Enter temperature (integer): 212

212 °F equals 100 °C

Enter temperature (integer): -40

-40 °F equals -40 °C


Section 4.8: What’s next


Back

Python Chrestomathics

Ahead

 


© Jim Cohoon. For now, all rights reserved.