I have posted homework 7 here.
It is due the Sunday at 11:59 PM.
for cels in range(min, max): #for loop range from 1 to 20 fah = 0 fah = cels * 9/5 + 32 #the formula to convert cels to fah print(cels, "\t",round(fah))
def FUNCTION_NAME([PARAMETER, ...], PARAMETER_NAME=DEFAULT_VALUE [, PARAMETER_NAME=DEFAULT_VALUE]):
STATEMENT
...
def convert(feet, inches, units='inches'):
def cheer(): print('Go ' + team) + '!') team = 'Red Sox' cheer()
>>> result = round(8.765)
>>> result
9
return EXPRESSION
return
is a Python
keyword
return
statement does two things
return
statement always causes the function to quitreturn
statement will never be executedreturn
statement can return more than one value
return EXPRESION, EXPRESSION [, EXPRESSION, ...]
>>> def get_full_name(): ... first_name = input("Please enter your first name: ") ... last_name = input("Please enter your last name: ") ... return first_name, last_name ...
>>> fname, lname = get_full_name() Please enter your first name: Glenn Please enter your last name: Hoffman
import
keyword
import random
>>> math.cos(math.pi) -1.0 >>> math.ceil(4.3)
$ cat random_1.py # demonstrates the use of the randint function of the random module import random for i in range(10): print(random.randint(1,100)) $ python3 random_1.py 89 98 93 73 32 40 63 100 76 80 $ python3 random_1.py 66 49 1 29 63 17 91 3 70 5
range
function$ cat random_5.py # demonstrates the use of the random function of the random module import random for i in range(5): print(random.random()) $ python3 random_5.py 0.3366683809865726 0.6243291094221154 0.47182435685723234 0.3079697111617222 0.5048399470937616
$ cat random_6.py # demonstrates the use of the uniform function of the random module import random for i in range(5): print(random.uniform(1, 5)) $ python3 random_6.py 3.987702460027857 2.776217942732739 2.890534381287354 1.5377836190792888 3.1461905324732022
$ cat random_6.py # demonstrates the use of the seed function in the random module # to generate the same series of numbers each time the program is run import random random.seed(67) for i in range(5): print(random.randint(1, 100)) $ python3 random_6.py 10 15 99 53 60 $ python3 random_6.py 10 15 99 53 60
round
returns either an integer or a floatinput
returns a stringdef in_range(min, max, value): if value < min: return False elif value > max: return False else: return True
True
if the value is greater or equal to the minimum def in_range(min, max, value): return value >= min and value <= max
value >= min and value <= maxas
min <= value <= max
$ cat in_range_3.py # demonstrates the use of a boolean data validation function def in_range(min, max, value): return min <= value <= max done = False while not done: number = int(input('Number: ')) if in_range(1, 5, number): print(number) done = True $ python3 in_range_3.py Number: 0 Number: 7 Number: 3 3
ls -l
on a directory it will list the contents alphabetically
$ ls -l
total 328
-rwxr-xr-x 1 glenn staff 6577 Jun 28 14:11 ex10.sh
-rwxr-xr-x 1 glenn staff 7780 Jun 28 14:11 ex11.sh
-rwxr-xr-x 1 glenn staff 4053 Jun 28 14:11 ex12.sh
-rwxr-xr-x 1 glenn staff 6208 Jun 28 14:11 ex13.sh
-rwxr-xr-x 1 glenn staff 9415 Jun 28 14:11 ex16.sh
-rwxr-xr-x 1 glenn staff 765 Jun 28 14:11 ex17.sh
-rwxr-xr-x 1 glenn staff 6653 Jun 28 14:11 ex18.sh
-rwxr-xr-x 1 glenn staff 8997 Jun 28 14:11 ex19.sh
-rwxr-xr-x 1 glenn staff 212 Jun 28 14:11 ex2.sh
-rwxr-xr-x 1 glenn staff 2075 Jun 28 14:11 ex20.sh
...
$ ls -l total 328 -rwxr-xr-x 1 glenn staff 212 Jun 28 14:08 ex02.sh -rwxr-xr-x 1 glenn staff 466 Jun 28 14:08 ex03.sh -rwxr-xr-x 1 glenn staff 595 Jun 28 14:08 ex04.sh -rwxr-xr-x 1 glenn staff 5474 Jun 28 14:08 ex05.sh -rwxr-xr-x 1 glenn staff 7428 Jun 28 14:08 ex06.sh -rwxr-xr-x 1 glenn staff 4182 Jun 28 14:08 ex07.sh -rwxr-xr-x 1 glenn staff 8919 Jun 28 14:08 ex08.sh -rwxr-xr-x 1 glenn staff 2407 Jun 28 14:08 ex09.sh -rwxr-xr-x 1 glenn staff 6577 Jun 28 14:08 ex10.sh ...
>>> import utilities >>> utilities.pad_number('5') '05'
# adds a '0' to a single digit and makes it a string def pad_number(number): number = int(number) if number < 10: return '0' + str(number)
>>> utilities.strip_zero('05') '5'
# first attempt at a game of rock paper scissors # asks the user for their move, checks that it is valid # and returns valid move def get_user_move(): move = input('Please chose rock (r), paper (p) or scissors (s): ') while move != 'r' and move != 'p' and move != 's': print(move, 'is not a valid choice') move = input('Please chose rock (r), paper (p) or scissors (s): ') return move print('You entered', get_user_move())
Please chose rock (r), paper (p) or scissors (s): q q is not a valid choice Please chose rock (r), paper (p) or scissors (s): r You entered r
# generates a move for the computer def get_computer_move(): move = random.randint(1,10000) if move % 3 == 1: return 'r' elif move % 3 == 2: return 'p' else: return 's'
for i in range(30): print(get_computer_move())
$ python3 rock_paper_scissors_2.py s r p p s s s s p ...
# turns a move represented by a letter into the appropriate word def move_letter_to_word(letter): if letter == 'r': return 'rock' elif letter == 'p': return 'paper' elif letter == 's': return 'scissors' else: return ''
for i in range(30): print(letter_to_word(get_computer_move()))
$ python3 rock_paper_scissors_3.py paper paper scissors rock ...
# returns the name of the winner def winner(user_move, computer_move): if user_move == computer_move: return "Tie" elif user_move == 'p' and computer_move == 'r': return "User" elif user_move == 'r' and computer_move == 'p': return "Computer" elif user_move == 'r' and computer_move == 's': return "User" elif user_move == 's' and computer_move == 'r': return "Computer" elif user_move == 's' and computer_move == 'p': return "User" elif user_move == 'p' and computer_move == 's': return "Computer"
print('r', 'r', winner('r', 'r')) print('p', 'p', winner('p', 'p')) print('s', 's', winner('s', 's')) print('p', 'r', winner('p', 'r')) print('r', 'p', winner('r', 'p')) print('r', 's', winner('r', 's')) print('s', 'r', winner('s', 'r')) print('s', 'p', winner('s', 'p')) print('p', 's', winner('p', 's'))
$ python3 rock_paper_scissors_4.py r r Tie p p Tie s s Tie p r User r p Computer r s User s r Computer s p User p s Computer
while
loopresult = "Tie" while result == "Tie": user_move = get_user_move() computer_move = get_computer_move() print('User chooses', letter_to_word(user_move)) print('Computer chooses', letter_to_word(computer_move)) result = winner(user_move, computer_move) if result != "Tie": print(result, 'wins') else: print("Tie")
Please chose rock (r), paper (p) or scissors (s): p User chooses paper Computer chooses paper Tie Please chose rock (r), paper (p) or scissors (s): r User chooses rock Computer chooses paper Computer wins
input
input('Please chose rock (r), paper (p) or scissors: ')
# asks the user for a move def get_move(): choice = input('Please chose rock (r), paper (p) or scissors (s): ') return choice
def get_user_move(): move = get_move() while move != 'r' and move != 'p' and move != 's': print(move, 'is not a valid choice') move = get_move() return move
while
loop is long and complicated# returns true if the input from the user specifying a move is valid def is_valid_move(move): return move == 'r' or move == 'p' or move == 's'
def get_user_move(): move = get_move() while not is_valid_move(move): print(move, 'is not a valid choice') move = get_move() return move