Python Codes

.pdf

School

Harvard University *

*We aren’t endorsed by this school

Course

228

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

5

Uploaded by LieutenantDragon138

Report
Rock paper scissors # Rock paper scissor game # 10/25/2021 import random player_score = 0 computer_score = 0 def rockPaperScissors(): global player_score global computer_score user_action = input("Enter a choice (rock[r], paper[p], scissors[s]): ") possible_actions = ["r", "p", "s"] computer_action = random.choice(possible_actions) if user_action not in possible_actions: print("Invalid input, try again") else: print(f"\nUser: {user_action}\nComputer: {computer_action}\n") if user_action == computer_action: print(f"Both players selected: {user_action}\nIt's a tie!") elif user_action == "r": if computer_action == "s": player_score += 1 print("Rock smashes scissors! You win!") else: computer_score += 1 print("Paper covers rock! You lose.") elif user_action == "p": if computer_action == "r": player_score += 1 print("Paper covers rock! You win!") else: computer_score += 1 print("Scissors cuts paper! You lose.") elif user_action == "s": if computer_action == "p": player_score += 1 print("Scissors cuts paper! You win!") else: computer_score += 1 print("Rock smashes scissors! You lose.") print ("\nYour score:", str(player_score)+ ", Computer score:", str(computer_score))
while True: rockPaperScissors() Prime numbers displayer # Prime number displayer def is_prime(integer): if (integer==1): return False elif (integer==2): return True else: for x in range(2,integer): if (integer % x == 0): return False return True for i in range(1,101): if is_prime(i): if i==97: print(i) else: print(i) Prime number checker # Prime number indicator def is_prime(integer): if (integer==1): return False elif (integer==2): return True else: for x in range(2,integer): if (integer % x == 0): return False return True Monthly tax sales
# Monthly tax calculator def tax(sales): stateTax=(sales*.05) countyTax=(sales*.025) totalSales=(sales-(stateTax+countyTax)) print(f"State sales tax is: ${stateTax:.2f}") print(f"County sales tax is: ${countyTax:.2f}") print(f"Your total monthly sales after tax: ${totalSales:.2f}") Merge sort algorithm # Merge sort algorithm def mergeSort(myList): if len(myList) > 1: mid = len(myList) // 2 left = myList[:mid] right = myList[mid:] # Recursive call on each half mergeSort(left) mergeSort(right) # Two iterators for traversing the two halves i = j = k = 0 # K is Iterator for the main list while i < len(left) and j < len(right): if left[i] <= right[j]: # The value from the left half has been used myList[k] = left[i] # Move the iterator forward i += 1 else: myList[k] = right[j] j += 1 # Move to the next slot k += 1 # For all the remaining values while i < len(left): myList[k] = left[i] i += 1 k += 1 while j < len(right): myList[k]=right[j]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help