# Examples: # If the user enters 'hi there' when pass_check() is called, prints # 'Invalid password' # If the user enters 'boB35' when pass_check() is called, prints 'Thank you!' # and 'boB35' is returned def pass_check(): my_password = str(input(welcome_msg)) if valid_password(my_password, 0): print(end_msg) return my_password else: print(invalid_msg) return pass_check() # Test Valid password check.set_input(["Ab3"]) check.set_screen("Thank you!") check.expect("Q1T1", pass_check(), "Ab3") # Test Invalid password, only contain number, lower-case or upper-case check.set_input(["abc", "123", "ABC","Af3"]) check.set_screen("Invalid password\nInvalid password\nInvalid password\nThank you!") check.expect("Q1T2", pass_check(), "Af3") # Test Invalid password, contain two of number, lower-case or upper-case check.set_input(["af3", "afFFG", "AF346", "AFfe3"]) check.set_screen("Invalid password\nInvalid password\nInvalid password\nThank you!") check.expect("Q1T3", pass_check(), "AFfe3")
print(countdown.format(correct, incorrect)) print("Sequence of" + " " + str_space + " " + "not found. Game Over.") return None elif n == 0 and secret != str_to_int: print(countdown.format(correct, incorrect)) print("Sequence of" + " " + str_space + " " + "not found. Game Over.") return None else: print(countdown.format(correct, incorrect)) return mastermind_helper(secret, turns - 1, n) ## Test check.set_input("1 2 3 4") check.set_print_exact( "There are 2 numbers in correct places and 0 numbers in incorrect places.", "Sequence of 1 6 1 4 not found. Game Over.") check.expect("Q4T11", mastermind_helper([1, 6, 1, 4], 1 - 1, 7), None) check.set_input("1 6 1 4") check.set_print_exact("Sequence found in 1 guesses.") check.expect("Q4T12", mastermind_helper([1, 6, 1, 4], 7 - 1, 7), 1) def mastermind(secret, turns): ''' Consumes a secret list of numbers (secret), the number of turns left (turns). Returns the number of guesses the guesser has left if there are still turns left or returns None when turns run out. And the
return 1 else: return 0 def check_space(n): if n == "": return 0 if n != " ": return 1 else: return 0 + check_space(n[1:]) ##Examples: check.set_input("goodPlace12!@@") check.set_print_exact(" Good") check.expect("Example 1:", new_password(), None) check.set_input("fairPlace !!!") check.set_print_exact(" Fair") check.expect("Example 2:", new_password(), None) check.set_input("weak12") check.set_print_exact(" Weak") check.expect("Example 3:", new_password(), None) ##Tests: check.set_input("1234") check.set_print_exact(" Weak") check.expect("Q4T1", new_password(), None)
print('Invalid input.') z = input('Enter frame: ') print((8 + x + 2 * y * (x + 2)) * z) print(z + (2 + x) * " " + "--" + y * ((2 * x + 2) * ' ' + '--') + ":~" + z) i = 0 while i < x: print(z + (1 + x - i) *" " + "/" + y*((2 + 2*i)*" " + "\\" + \ (2* (x - i ))*" " + "/") + ((4 + i) * " ") + z) i = i + 1 print(z + "--" + y * ((2 * x + 2) * " " + "--") + (4 + x) * " " + z) print((8 + x + 2 * y * (x + 2)) * z) #examples check.set_input('a', '1', '0', '#') check.set_screen('Invalid input.\n#########\n\ # --:~#\n# / #\n#-- #\n#########') check.expect("ex1", snake(), None) check.set_input('a', '0', '3', '1', '$$', "hello", "*") check.set_screen('Invalid input.\nInvalid input.\nInvalid input.\n\ Invalid input.\n*********************\n* -- --:~*\n\ * / \ / *\n* / \ / *\n* / \ / *\n\ *-- -- *\n*********************') check.expect("ex2", snake(), None) #tests check.set_input('1', '0', '#') check.set_screen('#########\n# --:~#\n# / #\n#-- #\n#########')
# check_password: Str Int(PRE: >=0) -> Bool # Conditions: # PRE: password is a nonempty string. # Purpose: produces True if the user enters the correct # password in limit attempts, and False otherwise. # Effects: The user enters a string,password when prompted. And # if the correct password is entered by the user, no more # attempts are given. # Examples: If the user enters "abc" when check_password("abc",2) is # called, True is produced. # If the user enters "ccc" when check_password("abc",1) is # called, False is produced. def check_password(password, limit): index = limit while index != 0: s = raw_input(prompt % index) if password == s: return True else: index -= 1 return False # Tests: check.set_input(['abc']) check.expect("Q2T1", check_password('abc',4),True) check.set_input(['aeo']) check.expect("Q2T2", check_password('abc',1),False) check.expect("Q3T3", check_password('tt',0),False)
fair_message = "Fair" good_message = "Good" password = input("Enter password: "******"goodPass12!@@") check.set_screen("Good") check.expect("Q4E1:", new_password(), None) ## Fair case check.set_input("fairPass !!!") check.set_screen("Fair") check.expect("Q4E2:", new_password(), None) ## Weak case check.set_input("weak12!") check.set_screen("Weak") check.expect("Q4E3:", new_password(), None) ## has a space, no numbers, only 1 alphanumeric check.set_input("Hello World!")
## a ## b ## c ## done ## ## results in 'output.txt' containing the following three lines ## ## ccccccccccccccccccccccc ## bbbbbbbbbbbbbbbbbbbbbbb ## aaaaaaaaaaaaaaaaaaaaaaa def silly(): with open("output.txt",'w') as outFile : ## silly_helper: None -> None ## ## Effects: Same as silly, but assumes the write file is already open def silly_helper() : nextLine = raw_input() if (nextLine == "done") : return silly_helper() outFile.write(nextLine*23) outFile.write("\n") silly_helper() ## Test by running function with varying number and types of input. # Be sure to do lots more of your own testing! check.set_input(['a','b','c','d','e',"done"]) check.set_file("output.txt","expected_output") check.expect("Simple test",silly(),None)
if computer_move == "r": print(general_message + "Computer won.") computer_win = computer_win + 1 n = n + 1 player_move = input(prompt) continue print(goodbye_message) print("Tied games:" + " " + str(tied_stats) + ".") print("Player won" + " " + str(player_win) + " " + "times.") print("Computer won" + " " + str(computer_win) + " " + "times.") # Testing # Test 1: Player enters "q" right away check.set_input(["q"]) check.set_screen("0 tied games, 0 player wins, 0 computer wins") check.expect("T1", rps(), None) # Test 2: One round, tie game check.set_input(["p", "q"]) check.set_screen("1 tie game, 0 player wins, 0 computer wins") check.expect("T2", rps(), None) # Test 3: One round, player wins check.set_input(["s", "q"]) check.set_screen("0 tied games, 1 player win, 0 computer wins") check.expect("T3", rps(), None) # Test 4: One round, computer wins check.set_input(["r", "q"])
check.set_screen('Prints a diamond, 11-d wide at middle') check.expect("diamondT1", print_diamond(5), None) # Situation 3: using raw_input # email: None -> str[len>=1] # Produces a "friendly" uw email address for the first and last name # entered by the user # Effects: Prompts the user for first and last name, and reads them in # Example: If the user enters "Lori" and "Case" as first and last names, # produces "*****@*****.**" def email(): first_name = raw_input("enter your first name:\n") last_name = raw_input("enter your last name:\n") email = first_name + '.' + last_name + '@uwaterloo.ca' return email check.set_input(["Lori", "Case"]) check.expect("emailT1", email(), '*****@*****.**') # Situation 4: creating files # add_word_count: str -> int[>=0] # Produces the total number of words in filename # Effects: opens, reads and closes filename. Also creates a new text file, # called wc-filename, containing the same contents as filename, but # each line is prepended by the number of words on that line. # Example: if small.txt contains # this file contains # # 5 words # then add_word_count("small.txt") => 5, and creates the file wc-small.txt, which # contains # ( 3 words): this file contains
# number_game: None -> None # Examples: # If the user input is ["40" "49" "39" "13" "10"], number_game() prints "Your # original number is 10" def number_game(): mul_4 = int(input(step1_msg)) my_num = mul_4/4 subtract_ori = plus_9(mul_4) divide_3 = sub_ori(subtract_ori, my_num) subtract_3 = div_3(divide_3) final_num = sub_3(subtract_3) print(final_msg.format(final_num)) # original number = 10 check.set_input(["40", "49", "39", "13", "10"]) check.set_screen("Your original number is 10") check.expect("original number = 10", number_game(), None) # original number = 7 check.set_input(["28", "32", "37", "30", "10", "7"]) check.set_screen("Think of a number. Multiply your number by 4:" "Add 9:" "That is not correct" "Subtract the original number:" "Divide by 3:" "Your original number is 7") check.expect("original number = 7", number_game(), None) # original number = 1 check.set_input(["4", "13", "12", "3", "4", "1"])
## if the user inputs '22aaB!!DF44##$', pass_check() prints Thank you! def pass_check(): enter = input(welcome_msg) if not(' ' in enter) and check_upper(enter) and \ check_lower(enter) and check_digit(enter): print(end_msg) return enter else: print(invalid_msg) return pass_check() ## Tests: check.set_input(['a', '3a', '3a A', '3aA']) check.set_screen("the user tried 3 times and succeeded in the last time") check.expect('t18', pass_check(), '3aA') check.set_input(['5344rffFEFA###22!!']) check.set_screen("the user succeed in the first time") check.expect('t19', pass_check(), '5344rffFEFA###22!!') check.set_input( ['aaAA', '2342A ERugfre 44 waterloo', '2342AERugfre44waterloo']) check.set_screen("the user tried 2 times and succeed after removing the space") check.expect('t20', pass_check(), '2342AERugfre44waterloo') check.set_input(['chen', 'CHEN', 'Chen', 'Chen387']) check.set_screen("the user succeeded in the last time") check.expect('t21', pass_check(), 'Chen387') check.set_input(['', ' ', 'C387Chen']) check.set_screen("the user succeeded in the last time") check.expect('t22', pass_check(), 'C387Chen')
# are met def pass_check(): s = input(welcome_msg) i = len(s) - 1 if check_lower(s, i) > 0 and check_upper(s, i) > 0 and check_num( s, i) > 0 and check_space(s, i): print(end_msg) else: print(invalid_msg) return pass_check() return s # Tests: check.set_screen('welcome_msg and end_msg are printed') check.set_input(['Boc123']) check.expect("Q1T1", pass_check(), 'Boc123') check.set_screen( 'welcome_msg and end_msg are printed with 1 error because of/ missing upper case' ) check.set_input(['boc123', 'Boc123']) check.expect("Q1T2", pass_check(), 'Boc123') check.set_screen( 'welcome_msg and end_msg are printed with 1 error because of/ missing lower case' ) check.set_input(['BOC123', 'Boc123']) check.expect("Q1T3", pass_check(), 'Boc123') check.set_screen(
## c ## done ## ## results in 'output.txt' containing the following three lines ## ## ccccccccccccccccccccccc ## bbbbbbbbbbbbbbbbbbbbbbb ## aaaaaaaaaaaaaaaaaaaaaaa def silly(): with open("output.txt", "w") as outFile: ## silly_helper: None -> None ## ## Effects: Same as silly, but assumes the write file is already open def silly_helper(): nextLine = raw_input() if nextLine == "done": return silly_helper() outFile.write(nextLine * 23) outFile.write("\n") silly_helper() ## Test by running function with varying number and types of input. # Be sure to do lots more of your own testing! check.set_input(["a", "b", "c", "d", "e", "done"]) check.set_file("output.txt", "expected_output") check.expect("Simple test", silly(), None)
return mastermind_helper(secret, turns - 1, guess) def mastermind(secret, turns): '''Returns the number of guesses the guesser takes to determine the sequence. Given a list of integers secret of size at least 2 and the maximum number of turns the guesser has to determine secretIf secret is not guessed in turns guesses, the function returns None. The file prompts the user to input a sequence of number to find out the number of elements in two lists that are in the same place, and determining the number of elements in two lists that match but are in different locations. mastermind: (listof Int) Nat -> (Anyof None Nat) Requires: ''' return mastermind_helper(secret, turns, turns) #Example 1 check.set_input(' 1 2 3 4', ' 6 2 3 5', ' 1 1 6 4', '1 6 1 4') check.set_print_exact( 'There are 2 numbers in correct places and 0 numbers \ in incorrect places.', 'There are 0 numbers in correct places and 1 numbers \ in incorrect places.', 'There are 2 numbers in correct places and 2 numbers \ in incorrect places.', 'Sequence found in 4 guesses.') check.expect('Example1', mastermind([1, 6, 1, 4], 5), 4)
print("Enter 1 to take " + str(board[0])) print("Enter 2 to take " + str(board[1])) print("Enter 3 to take " + str(board[2])) print("Enter 4 to take " + str(board[3])) player_input = int(input(prompt)) taken_row = board[player_input - 1] board[player_input - 1] = [card] return taken_row smallest_index = list_of_diff.index(min_diff) if len(board[smallest_index]) == full_row: old_row = board[smallest_index] board[smallest_index] = [card] return old_row else: board[smallest_index].append(card) return True # Q4 Test 1 card smaller than all last elements: check.set_screen('"Enter (row number) to take my_board[row number - 1]" for all 4 rows followed \ by "Enter choice: "') check.set_input(["3"]) check.expect("T1", turn_6nimmt(my_board, 4), [8]) check.expect("T1{my_board}", my_board, [[67, 70], [9, 18, 19], [4], [13, 30, 50, 88, 93]]) # Q4 Test 2 card on full row check.expect("T2", turn_6nimmt(my_board, 94), [13, 30, 50, 88, 93]) check.expect("T2{my_board}", my_board, [[67, 70], [9, 18, 19], [4], [94]]) # Q4 Test 3 card on not full row check.expect("T3", turn_6nimmt(my_board, 20), True) check.expect("T3{my_board}", my_board, [[67, 70], [9, 18, 19, 20], [4], [94]])
comp_ans_num = fib_rec(game_num) % 3 if comp_ans_num == 0: comp_ans = "r" elif comp_ans_num == 1: comp_ans = "p" else: comp_ans = "s" if (player_ans == "r" and comp_ans == "p") or (player_ans == "p" and comp_ans == "s") or (player_ans == "s" and comp_ans == "r"): comp_wins = comp_wins + 1 print("Player plays: " + player_ans + ". Computer plays: " + comp_ans + ". Computer won.") elif (player_ans == "p" and comp_ans == "r") or (player_ans == "s" and comp_ans == "p") or (player_ans == "r" and comp_ans == "s"): player_wins = player_wins + 1 print("Player plays: " + player_ans + ". Computer plays: " + comp_ans + ". Player won.") elif player_ans == comp_ans: tied_games = tied_games + 1 print("Player plays: " + player_ans + ". Computer plays: " + comp_ans + ". Tied.") print("Thank you for playing! Total game statistics:") print("Tied games: " + str(tied_games) + ".") print("Player won " + str(player_wins) + " times.") print("Computer won " + str(comp_wins) + " times.") # Q4 Test 1: one response check.set_screen("Welcome to the Rock-Paper-Scissors game!\nHow would you like to play next [r/p/s/q]?\nPlayer plays: r. \ Computer plays: p. Computer won.\nHow would you like to play next [r/p/s/q]?\nThank you for playing! Total game statistics:\nTied games 0.\nPlayer won 0 times.\nComputer won 1 times.") check.set_input(["r","q"]) check.expect("T1", play_rps(), None) # Q4 Test 2: many responses check.set_screen("Welcome to the Rock-Paper-Scissors game!\nHow would you like to play next [r/p/s/q]?\nPlayer plays: r. Computer plays: p. Computer won.\nHow would you like to play next [r/p/s/q]?\nPlayer plays: s. Computer plays: p. Player won.\nHow would you like to play next [r/p/s/q]?\nPlayer plays: p. Computer plays: s. Computer won.\nHow would you like to play next [r/p/s/q]?\nPlayer plays: s. Computer plays: r. Computer won.\nHow would you like to play next [r/p/s/q]?\nThank you for playing! Total game statistics:\nTied games 0.\nPlayer won 1 times.\nComputer won 3 times.") check.set_input(["r","s","p","s","q"]) check.expect("T2", play_rps(), None)
if card - last_card4 == row_to_enter and card > last_card4 and len( board[3]) == 5: old_row = board[3] board[3] = [card] return old_row # Tests for turn_6nimmt check.expect('Q4T1a', turn_6nimmt(my_board, 71), True) check.expect('Q4T1b', my_board, [[67, 70, 71], [9, 18, 19], [8], [13, 30, 50, 88, 93]]) check.expect('Q4T2a', turn_6nimmt(my_board, 94), [13, 30, 50, 88, 93]) check.expect('Q4T2b', my_board, [[67, 70, 71], [9, 18, 19], [8], [94]]) check.expect('Q4T3a', turn_6nimmt(my_board, 25), True) check.expect('Q4T3b', my_board, [[67, 70, 71], [9, 18, 19, 25], [8], [94]]) check.set_input(['2']) check.set_screen('Choose one of the rows') check.expect('Q4T4a', turn_6nimmt(my_board, 5), [9, 18, 19, 25]) check.expect('Q4T4b', my_board, [[67, 70, 71], [5], [8], [94]]) check.expect('Q4T5a', turn_6nimmt(my_board, 9), True) check.expect('Q4T5b', my_board, [[67, 70, 71], [5], [8, 9], [94]]) check.expect('Q4T6a', turn_6nimmt(my_board, 10), True) check.expect('Q4T6b', my_board, [[67, 70, 71], [5], [8, 9, 10], [94]]) check.expect('Q4T7a', turn_6nimmt(my_board, 11), True) check.expect('Q4T7b', my_board, [[67, 70, 71], [5], [8, 9, 10, 11], [94]]) check.set_input(['3']) check.set_screen('Choose one of the rows') check.expect('Q4T8a', turn_6nimmt(my_board, 2), [8, 9, 10, 11]) check.expect('Q4T8b', my_board, [[67, 70, 71], [5], [2], [94]]) check.expect('Q4T9a', turn_6nimmt(my_board, 72), True) check.expect('Q4T9b', my_board, [[67, 70, 71, 72], [5], [2], [94]])
length = input("Enter length: ") while (length.isdigit() == False): print('Invalid input.') length = input("Enter length: ") f = input("Enter frame: ") while (len(f) != 1): print('Invalid input.') f = input("Enter frame: ") if height.isdigit() and length.isdigit() and (len(f) == 1): frame(int(height),int(length),f) else: print('Invalid input.') snake() # Examples check.set_input('1','0','#') check.set_print_exact('#########\n'+ '# --:~#\n'+ '# / #\n'+ '#-- #\n'+ '#########') check.expect('Q4T2', snake(),None) check.set_input('a','0','3','1','$$','hello','*') check.set_print_exact('Invalid input.', 'Invalid input.', 'Invalid input.', 'Invalid input.', '*********************\n' + '* -- --:~*\n' + '* / \ / *\n' +
"Enter password:"******"Enter password:"******"oodPass12!@@") check.set_print_exact("Good") check.expect("Test1", new_password(), None)
value = D.pop(tar_dec) D.update({tar_dec: value-1}) if cmd == 'A': all_value = reduce(lambda x,y: x+y, D.values()) count = len(D) outcome = float(all_value)/count print '%.2f' %outcome cmd = raw_input(cmd_prompt) for key in sorted(D): value = str(D[key]) print key, value # Test ## average of the values in dictionary D = {'mary':6,'bob':3} check.set_input(['A']) check.set_screen(4.50) check.expect('q4t1a', memory(D), None) check.expect('q4t1b', D, D) ## increse one of the value in dictionary by 1 check.set_input(['I', 'mary']) check.expect('q4t2a', memory(D), None) check.expect('q4t2b', D, {'mary':7,'bob':3}) ## compare the two key values, G check.set_input(['G', 'mary','bob']) check.set_screen(True) check.expect('q4t3a', memory(D), None) check.expect('q4t3b', D, D) ## compare the two key values, L check.set_input(['L', 'mary','bob']) check.set_screen(False)
## Effects: ##* one value is read in ##* A string is printed ##* If the number is correctly entered another string will be printed ##* The final string printed will give the original number ## number_game: Null -> Null ## Examples: # If the user enters 32 when the program is run. The program will guide the user # to do a few calculations though prints. The program will then print the number # the user was originally thinking of, which is 8 def number_game(): msg1 = int(input(step1_msg)) print(step2(msg1)) ## Tests: check.set_input(["32", "41", "33", "11", "8"]) check.set_screen("PROMPTS THEN: Your original number is 8") check.expect("Test1", number_game(), None) check.set_input(["24", "0", "45", "33", "27", "9", "6"]) check.set_screen("PROMPTS AND ERROR MESSAGES THEN: Your original number is 6") check.expect("Test2", number_game(), None) check.set_input(["28", "37", "30", "10", "7"]) check.set_screen("PROMPTS THEN: Your original number is 7") check.expect("Test3", number_game(), None)
# pass_check: None -> Str # Examples: # If the user enters 'hi there' when pass_check() is called, prints # 'Invalid password' # If the user enters 'boB35' when pass_check() is called, prints 'Thank you!' # and 'boB35' is returned def pass_check(): my_password = str(input(welcome_msg)) if valid_password(my_password, 0): print(end_msg) return my_password else: print(invalid_msg) return pass_check() # Test Valid password check.set_input(["Ab3"]) check.set_screen("Thank you!") check.expect("Q1T1", pass_check(), "Ab3") # Test Invalid password, only contain number, lower-case or upper-case check.set_input(["abc", "123", "ABC","Af3"]) check.set_screen("Invalid password\nInvalid password\nInvalid password\nThank you!") check.expect("Q1T2", pass_check(), "Af3") # Test Invalid password, contain two of number, lower-case or upper-case check.set_input(["af3", "afFFG", "AF346", "AFfe3"]) check.set_screen("Invalid password\nInvalid password\nInvalid password\nThank you!") check.expect("Q1T3", pass_check(), "AFfe3")
# the number from last step def pass_to_c5(c4): global c5 c5 = input(step5_msg) if int(c5) == int(c4) - 3: print(final_msg.format(c5)) else: print(error_msg) pass_to_c5(c4) # Tests: check.set_screen( 'From step1_msg to step5_msg, and "Your orginal number is / 10" are printed' ) check.set_input(["40", "49", "39", "13", "10"]) check.expect("Q3T1", number_game(), None) check.set_screen( 'From step1_msg to step5_msg with and error between step1/ and 2, and "Your orginal number is 10" are printed' ) check.set_input(["40", "43" "49", "39", "13", "10"]) check.expect("Q3T2", number_game(), None) check.set_screen( 'From step1_msg to step5_msg with and error between step2/ and 3, and "Your orginal number is 10" are printed' ) check.set_input(["40", "49", "34" "39", "13", "10"]) check.expect("Q3T3", number_game(), None) check.set_screen(